Request类
urlopen()方法只能构建简单的表请求,如果需要Headers等信息,必须使用Request类来构建
Request类的使用
实例化对象 = urllib.request.Request("请求路径")
示例代码
import urllib.request
# 实例化Request类
request = urllib.request.Request("https://www.python.org/")
# 发起请求
response = urllib.request.urlopen(request)
print(response.read().decode())
Request类的参数
参数作用:
url:必传参数,其他都是可选参数,传的是url路径
data:必须传bytes数据类型,如果是字典的格式可以使用:parse.urlencode(字典)解析为url格式
headers:伪装浏览器请求头
origin_req_host:请求方的host名称或者ip地址
unverifiable:请求是否是无法验证,默认为false, 意思是说用户没有足够权限来选择接收请求的结果
method:指示请求使用方法: GET POST
示例代码:
from urllib import request, parse
url = "http://httpbin.org/post"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
'Host': "httpbin.org"
}
d = {
"name": 'Germey'
}
data = bytes(parse.urlencode(d), encoding="utf8")
req = request.Request(url=url, headers=headers, data=data, method='POST')
response = request.urlopen(req)
print(response.read().decode())
运行结果:
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "Germey"
},
"headers": {
"Accept-Encoding": "identity",
"Content-Length": "11",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
},
"json": null,
"origin": "173.82.187.193, 173.82.187.193",
"url": "https://httpbin.org/post"
}