DrissionPage XHR请求扩展库,支持多种数据类型和请求方式
Project description
XHR请求模块使用说明
概述
这是一个基于DrissionPage的XHR请求封装模块,支持多种数据类型和请求方式。相比原版本,新版本支持:
- ✅ 多种数据格式:表单数据、JSON数据、原始文本、二进制数据
- ✅ 自定义请求头
- ✅ 完整的HTTP方法支持:GET、POST、PUT、PATCH、DELETE
- ✅ 统一的API接口
- ✅ 更好的错误处理
主要改进
1. 统一的request方法
def request(self, method: str, url: str,
data: Optional[Union[Dict[str, Any], str, bytes]] = None,
json_data: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None,
timeout: int = 30000) -> XHRResponse
2. 支持多种数据类型
- Dict: 自动转换为表单数据 (
application/x-www-form-urlencoded) - str: 作为原始文本发送 (
text/plain) - bytes: 作为二进制数据发送 (
application/octet-stream) - json_data: 作为JSON发送 (
application/json)
3. 便捷方法
get(url, headers=None, timeout=30000)post(url, data=None, json_data=None, headers=None, timeout=30000)put(url, data=None, json_data=None, headers=None, timeout=30000)patch(url, data=None, json_data=None, headers=None, timeout=30000)delete(url, headers=None, timeout=30000)
使用示例
基本用法
from DrissionPage import ChromiumPage
from xhr_request import XHRClient
# 创建浏览器实例
page = ChromiumPage()
client = XHRClient(page)
# GET请求
response = client.get('https://api.example.com/data')
print(f"状态码: {response.status_code}")
print(f"响应: {response.text}")
发送表单数据
# 方式1:使用post方法
form_data = {'username': 'user', 'password': 'pass'}
response = client.post('https://api.example.com/login', data=form_data)
# 方式2:使用通用request方法
response = client.request('POST', 'https://api.example.com/login', data=form_data)
发送JSON数据
# 方式1:使用post方法
json_data = {'name': '张三', 'age': 25}
response = client.post('https://api.example.com/users', json_data=json_data)
# 方式2:使用通用request方法
response = client.request('POST', 'https://api.example.com/users', json_data=json_data)
发送原始文本
text_data = "这是一些原始文本数据"
response = client.post('https://api.example.com/text', data=text_data)
发送二进制数据
binary_data = b'\x00\x01\x02\x03\x04\x05'
response = client.post('https://api.example.com/binary', data=binary_data)
自定义请求头
headers = {
'Authorization': 'Bearer your-token',
'User-Agent': 'Custom-Client/1.0',
'X-API-Key': 'your-api-key'
}
# GET请求带自定义头
response = client.get('https://api.example.com/protected', headers=headers)
# POST请求带自定义头
response = client.post('https://api.example.com/data',
json_data={'key': 'value'},
headers=headers)
其他HTTP方法
# PUT请求
response = client.put('https://api.example.com/resource/1',
json_data={'update': 'data'})
# PATCH请求
response = client.patch('https://api.example.com/resource/1',
json_data={'field': 'new_value'})
# DELETE请求
response = client.delete('https://api.example.com/resource/1',
headers={'Authorization': 'Bearer token'})
响应处理
response = client.get('https://api.example.com/data')
# 检查请求是否成功
if response.ok:
print("请求成功")
else:
print(f"请求失败,状态码: {response.status_code}")
# 获取响应文本
print(response.text)
# 解析JSON响应
try:
data = response.json
print(data)
except ValueError:
print("响应不是有效的JSON格式")
# 获取响应头
print(response.headers)
错误处理
try:
response = client.post('https://api.example.com/data',
json_data={'key': 'value'})
if response.ok:
print("请求成功")
else:
print(f"HTTP错误: {response.status_code}")
except Exception as e:
print(f"请求失败: {e}")
注意事项
- 依赖: 需要安装DrissionPage:
pip install DrissionPage - 浏览器: 需要Chromium浏览器实例运行
- 同步请求: 当前实现使用同步XHR请求
- 编码: 自动处理UTF-8编码
- 超时: 默认超时时间为30秒
迁移指南
如果你在使用旧版本,以下是主要变更:
旧版本
# 表单数据
response = client.request('POST', url, form_data={'key': 'value'})
# JSON数据
response = client.json_request('POST', url, {'key': 'value'})
新版本
# 表单数据
response = client.post(url, data={'key': 'value'})
# JSON数据
response = client.post(url, json_data={'key': 'value'})
新版本向后兼容,但建议使用新的API以获得更好的功能支持。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drissionpage_expend-1.0.1.tar.gz
(15.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drissionpage_expend-1.0.1.tar.gz.
File metadata
- Download URL: drissionpage_expend-1.0.1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
709abcd15b8595cecab7ab103d2f860496e085c83cbb10a9cdd53746be839b9d
|
|
| MD5 |
788f859a235d15c799a6e14753d015d6
|
|
| BLAKE2b-256 |
4053f4360a2569761dd487f0dd0ac9a3025b700280918490046f7ac90ec1f63c
|
File details
Details for the file drissionpage_expend-1.0.1-py3-none-any.whl.
File metadata
- Download URL: drissionpage_expend-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
885b9b428f83938124057c42716ed40427c52b348d76187e532aafef13104c42
|
|
| MD5 |
dc938f12f6d7cadec0e4e7cfb0188243
|
|
| BLAKE2b-256 |
318061c3a64a3e0e10c6c09577582f2cc3e0c00a588af20d4c82d8a3ca2f1f87
|