解码URL中的转义字符
我有一个列表,里面包含了一些网址,这些网址中有一些字符被转义了。这个转义是通过 urllib2.urlopen
在获取网页时自动处理的:
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=edit
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=history
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&variant=zh
有没有办法把这些转义的字符转换回它们原来的样子呢?
附注:这些网址是用utf-8编码的。
5 个回答
18
或者使用 urllib.unquote_plus
这个方法
>>> import urllib
>>> urllib.unquote('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte+membrane+protein+1,+PfEMP1+(VAR)'
>>> urllib.unquote_plus('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte membrane protein 1, PfEMP1 (VAR)'
39
如果你在使用 Python3
,你可以这样做:
import urllib.parse
urllib.parse.unquote(url)