我想用特殊字符对URL进行编码。在我的例子中它是:š, ä, õ, æ, ø
(它不是有限列表)。
urllib2.quote(symbol)
给出了非常奇怪的结果,这是不正确的。这些符号怎么可以编码?
答案 0 :(得分:8)
urllib2.quote("Grønlandsleiret, Oslo, Norway")
提供%27Gr%B8nlandsleiret%2C%20Oslo%2C%20Norway%27
然后明确使用UTF-8:
urllib2.quote(u"Grønlandsleiret, Oslo, Norway".encode('UTF-8'))
始终在您的文件中说明编码。请参阅PEP 0263。
非UTF-8字符串需要首先解码,然后编码:
# You've got a str "s".
s = s.decode('latin-1') # (or what the encoding might be …)
# Now "s" is a unicode object.
s = s.encode('utf-8') # Encode as UTF-8 string.
# Now "s" is a str again.
s = urllib2.quote(s) # URL encode.
# Now "s" is encoded the way you need it.