urllib

2018-07-27  本文已影响9人  好小葱1

urllib.quote

对字符串进行编码
data = 'name = ~a+3'  

data1 = urllib.quote(data)   
print data1 # result: name%20%3D%20%7Ea%2B3

urllib.unquote

对字符串进行解码
print urllib.unquote(data1) # result: name = ~a+3

urllib.urlencode

把key-value键值进行编码
>>> from urllib import urlencode
>>> data = {
...     'a': 'test',
...     'name': '魔兽'
... }
>>> print urlencode(data)
a=test&amp&name=%C4%A7%CA%DE

没有urldecode,解码的时候只有结合urlparse.parse_qs使用

上的几个几个包,在python 3版本都放在了urllib.parse下面

requests VS urllib

参考:https://stackoverflow.com/questions/2018026/what-are-the-differences-between-the-urllib-urllib2-and-requests-module

Requests - Requests' is a simple, easy-to-use HTTP library written in Python. 1) Python Requests encodes the parameters automatically so you just pass them as simple arguments, unlike in the case of urllib, where you need to use the method urllib.encode() to encode the parameters before passing them.

上一篇下一篇

猜你喜欢

热点阅读