python urllib的urlencode和quote和quote_plus

http://www.douban.com/note/491745257/

背景:需要使用一个有些奇葩的第三方接口。
python部分:
1.urllib.urlencode(query, doseq=0)
接受参数形式为:[(key1, value1), (key2, value2),...] 和 {'key1': 'value1', 'key2': 'value2',...}
返回的是形如'key2=value2&key1=value1'字符串。
>>>urllib.urlencode({'name': u'老王'.encode('utf8'), 'sex': u'男'.encode('utf8')})
'name=%E8%80%81%E7%8E%8B&sex=%E7%94%B7'

2.urllib.quote(s, safe='/')
接受参数s为字符串,safe是指定某字符不被urlencode,默认为'/',
如指定'+'、'/'不需转换,传 '+/' 和 '+ /' 均可。另外此方法会将“空格”转换为“%20”
>>> urllib.quote(u'老王 /+'.encode('utf8'))
'%E8%80%81%E7%8E%8B%20/%2B'

3.urllib.quote_plus(s, safe='')
此方法的源码为:
def quote_plus(s, safe=''):
    """Quote the query fragment of a URL; replacing ' ' with '+'"""
    if ' ' in s:
        s = quote(s, safe + ' ')
        return s.replace(' ', '+')
    return quote(s, safe)
可以看出它比quote多一些功能,但是会将“空格”转换成“加号”,默认safe为空。
>>> urllib.quote_plus(u'老王 /+'.encode('utf8'))
'%E8%80%81%E7%8E%8B+%2F%2B'

具体使用哪个方法,看需求。

urlencode部分:
并不是所有相关字符都需要转码,有哪些字符需要urlencode并且为什么?
ASCII Control characters
Why: These characters are not printable.
Characters: Includes the ISO-8859-1 (ISO-Latin) character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal.)

Non-ASCII characters
Why: These are by definition not legal in URLs since they are not in the ASCII set.
Characters: Includes the entire "top half" of the ISO-Latin set 80-FF hex (128-255 decimal.)

"Reserved characters"
Why: URLs use some characters for special use in defining their syntax. When these characters are not used in their special role inside a URL, they need to be encoded.
Characters: $ & + ; / : , = ? @

"Unsafe characters"
Why: Some characters present the possibility of being misunderstood within URLs for various reasons. These characters should also always be encoded.
Characters:< > # % { } | \ ~ ^ [ ] `

详见:URL Encoding (or: 'What are those "%20" codes in URLs?')

posted @ 2015-12-23 15:30  jimshi  阅读(1953)  评论(0编辑  收藏  举报