相对于urllib.request.urlopen()来说urllib.request.Request是进一步的包装请求,下面是请求类的源码示例:
class Request:
# 主要看这块,构造函数中指明了Request进一步包装请求中可以传递的参数有(url,data,headers,
# origin_req_host,unverifiable,method)
def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False,
method=None):
self.full_url = url
self.headers = {}
self.unredirected_hdrs = {}
self._data = None
self.data = data
self._tunnel_host = None
for key, value in headers.items():
self.add_header(key, value)
if origin_req_host is None:
origin_req_host = request_host(self)
self.origin_req_host = origin_req_host
self.unverifiable = unverifiable
if method:
self.method = method
pass
我们可以这样使用(以下是模拟有道字典翻译发送的请求):
# 请求地址url
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
# 请求头
request_headers = {
'Host':'fanyi.youdao.com',
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
}
# 发送给服务器的表单
form_data = {
"i": word,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME",
"typoResult": "false"
}
# POST发送的data必须为bytes或bytes类型的可迭代对象,不能是字符串
form_data = urllib.parse.urlencode(form_data).encode()
# 构造请求对象Request
req = urllib.request.Request(url, data=form_data, headers=request_headers)
# 发起请求
response = urllib.request.urlopen(req)
data = response.read().decode()
print(data)
所以,总的来说,如果我们在获取请求对象时,不需要过多的参数传递,我么可以直接选择urllib.request.urlopen();如果需要进一步的包装请求,则需要用urllib.request里。的urlopen()进行包装处理。