开发者

How to properly url encode accents?

I need to url encode foreign names, like "Misère".

When I do:

urllib2.quote(name)

I get a the e开发者_Python百科rror:

File "/System/Library/Frameworks/Python.framework/Versions/
2.5/lib/python2.5/urllib.py", line 1205, in quote
    res = map(safe_map.__getitem__, s)
KeyError: u'\xe8'

What am I doing wrong?


Try:

urllib2.quote(s.encode('utf8'))


A slight improvement to @苏妍倩's answer would be to include safe characters in the method call. By default, urllib2.quote() only includes / _ - . as a safe character which means characters like : will be converted, rendering the url useless.

For example:

url = https://www.zomato.com/pittsburgh/caffè-damore-catering-pittsburgh
print urllib2.quote(url.encode('utf-8'))
>>> https%3A//www.zomato.com/pittsburgh/caff%C3%A8-damore-catering-pittsburgh

print urllib2.quote(url.encode('utf-8'),':/')
>>> https:////www.zomato.com/pittsburgh/caff%C3%A8-damore-catering-pittsburgh

Notice the slight difference in outputs in the https portion of the url.

Hopefully this saves someone else the time it took me to figure this out!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜