简体   繁体   中英

Python encode url with special characters

I want to encode URL with special characters. In my case it is: š, ä, õ, æ, ø (it is not a finite list).

urllib2.quote(symbol) gives very strange result, which is not correct. How else these symbols can be encoded?

urllib2.quote("Grønlandsleiret, Oslo, Norway") gives a %27Gr%B8nlandsleiret%2C%20Oslo%2C%20Norway%27

Use UTF-8 explicitly then:

urllib2.quote(u"Grønlandsleiret, Oslo, Norway".encode('UTF-8'))

And always state the encoding in your file. See PEP 0263 .


A non-UTF-8 string needs to be decode first, then encoded:

                           # 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM