0

When I parsed a urllib, I got a UnicodeEncodeError. Follows are my codes.

import urllib.request, urllib.parse, urllib.error   
def read_test():
    quotes = open("C:\\movie_quotes.txt")
    contents_of_file = quotes.read()
    print(contents_of_file)
    quotes.close()
    check_profanity(contents_of_file)

def check_profanity(text_to_check):
    connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+text_to_check)
   output = connection.read()
   print(output)
   connection.close()

if __name__ == '__main__':
    read_test()     
Aaron
  • 1
  • Which line generates the error? And which version of Python? – Mark Ransom Apr 21 '17 at 02:54
  • 7 and 10 lines. – Aaron Apr 21 '17 at 02:59
  • Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – DYZ Apr 21 '17 at 03:01
  • I read that, but I can not understand a lot. Would you like to type correct codes for me? – Aaron Apr 21 '17 at 03:14
  • Consider using `codecs.open`. See http://stackoverflow.com/a/12468274/2904896 – metatoaster Apr 21 '17 at 04:03
  • You should include the stack trace in your post, and you need to tell us your Python version, as already requested by Mark. Most probably the problem is that you're creating a URL with an arbitrary string. You need to [urlencode](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode) that first. – lenz Apr 21 '17 at 07:29

0 Answers0