python url编码-----urllib的urlencode和quote和quote_plus

 

背景:需要使用有特别要求的第三方接口。

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:< > # % { } | \ ~ ^ [ ] `

 

 

另外,值得注意的是:

base_url = 'https://s.taobao.com/search?'
data = {
    'q': 'ipad'
}

print('https://s.taobao.com/search?q='+quote('ipad'))
print(base_url+urlencode(data))
"""

quote 和 urlencode 的区别 urlencode 需要用字典  

quote单个字符就行了,因为quote只需要字符串就行了

https://s.taobao.com/search?ipad
https://s.taobao.com/search?q=ipad
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值