Skip to content
Advertisement

python urllib2 and unicode

I would like to collect information from the results given by a search engine. But I can only write text instead of unicode in the query part.

JavaScript

give this error

JavaScript

Advertisement

Answer

Encode the Unicode data to UTF-8, then URL-encode:

JavaScript

Demo:

JavaScript

Using urllib.urlencode() to build the parameters is easier, but you can also just escape the query value with urllib.quote_plus():

JavaScript
Advertisement