urllib库练习
主要介绍urllib库相关模板练习及代码展示。
相关代码
from urllib import request, parse
url = ‘http://httpbin.org/post’
headers = {
‘User-Agent’: ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)’,
‘Host’: ‘httpbin.org’
}
dicts = {
‘name’: ‘Germey’
}
data = bytes(parse.urlencode(dicts), encoding=‘utf8’)
req = request.Request(url=url, data=data, headers=headers, method=‘POST’)
response = request.urlopen(req)
print(response.read().decode(‘utf8’))