python3 urllib.request.Request的用法

import urllib.request
import urllib.parse

url = 'http://127.0.0.1:8000/api/login/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
headers['Host'] = 'httpbin.org'
dict = {'user': 'admin','pwd': '12345',}

data = urllib.parse.urlencode(dict).encode('utf-8')
#data参数如果要传必须传bytes(字节流)类型的,如果是一个字典,先用urllib.parse.urlencode()编码。
request = urllib.request.Request(url = url,data = data,headers = headers,method = 'POST')

response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')

print(html)
————————————————
版权声明:本文为CSDN博主「开开136」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kaikai136412162/java/article/details/83142711

原文地址:https://www.cnblogs.com/zhoading/p/12689165.html