How to: Unicode in URL with Python 3

I found this to be much more difficult than I thought it would be. The solution was simple, but finding it was a bit of a pain. Here is the solution:

from urllib.parse import quote
from urllib.request import urlopen

url_string = "http://your_site_here" + quote(SOME-STRING-WITH-UNICODE)
html = urlopen(url_string).read().decode('utf-8')

Extremely simple, but it caused me a fair amount of headache to figure that out.

Common errors that might bring you to this post:

AttributeError: ‘bytes’ object has no attribute ‘timeout’
TypeError: Can’t convert ‘bytes’ object to str implicitly
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xf3’ in position 6: ordinal not in range(128)
TypeError: a bytes-like object is required, not ‘str’
TypeError: expected bytes-like object, not str


Leave a Reply