urllib.request.Request对象封装请求

一些服务器只允许普通浏览器的请求而不允许来自脚本的请求,可以使用urllib.request.Request对象封装请求头的信息(请求头需要以字典的形式封装),伪装成普通浏览器发送请求。

urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

headers字典的键值对会作为参数调用add_header()方法,详细见python官方文档:

https://docs.python.org/3.12/library/urllib.request.html

user-agent请求头信息查看方法

1. http://httpbin.org/get可以帮助开发者测试http响应和请求

2. 浏览器开发者工具查看

使用httpbin工具进行请求响应测试

未封装请求头
 from urllib.request import Request, urlopen
# 请求地址
url = 'http://httpbin.org/get'
# 创建request对象
req = Request(url)
# 发送请求
resp = urlopen(req)
# 读取响应内容
print(resp.read().decode())
# 关闭连接
resp.close()

响应结果:

{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Host": "httpbin.org", 
    "User-Agent": "Python-urllib/3.12", 
    "X-Amzn-Trace-Id": "Root=1-66e2bac5-5ae0d2f95970fd2938a9976d"
  }, 
  "origin": "111.8.73.196", 
  "url": "http://httpbin.org/get"
}
封装请求头的user-agent信息
from urllib.request import Request, urlopen
# 请求地址
url = 'http://httpbin.org/get'
# 创建request对象
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36'
headers = {'user-agent': user_agent}
req = Request(url, headers=headers)
# 发送请求
resp = urlopen(req)
# 读取响应内容
print(resp.read().decode())
# 关闭连接
resp.close()

响应结果:

{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-66e2c23c-20813ff66fbcba0e293b3ee0"
  }, 
  "origin": "111.8.73.196", 
  "url": "http://httpbin.org/get"
}
posted @ 2024-09-12 18:23  松鼠q  阅读(18)  评论(0编辑  收藏  举报