简介:Curl 是一个强大的命令行工具,用于传输数据。在开发爬虫或调用 API 时,我们经常需要将 Curl 命令转换为 Python 代码。本文将详细介绍如何将各种类型的 Curl 命令转换为 Python Requests 代码。
一、为什么需要 Curl 转 Python
在实际开发中,我们经常遇到以下场景:
- 浏览器开发者工具中复制为 Curl 命令
- API 文档提供 Curl 示例
- 需要快速测试 HTTP 请求
- 团队协作中分享请求示例
二、基础 GET 请求转换
最简单的 Curl GET 请求:
curl "https://api.example.com/users"
对应的 Python 代码:
import requests
response = requests.get("https://api.example.com/users")
print(response.text)
三、带参数的 GET 请求
Curl 命令:
curl "https://api.example.com/search?q=python&page=1"
Python 代码(推荐方式):
import requests
params = {
"q": "python",
"page": 1
}
response = requests.get("https://api.example.com/search", params=params)
print(response.text)
四、POST 请求转换
发送 JSON 数据的 POST 请求:
curl -X POST "https://api.example.com/users" \
-H "Content-Type: application/json" \
-d '{"name": "张三", "email": "zhangsan@example.com"}'
Python 代码:
import requests
import json
url = "https://api.example.com/users"
headers = {
"Content-Type": "application/json"
}
data = {
"name": "张三",
"email": "zhangsan@example.com"
}
response = requests.post(url, headers=headers, json=data)
print(response.text)
五、处理请求头
Curl 命令:
curl "https://api.example.com/data" \
-H "Authorization: Bearer token123" \
-H "User-Agent: MyApp/1.0" \
-H "Accept: application/json"
Python 代码:
import requests
headers = {
"Authorization": "Bearer token123",
"User-Agent": "MyApp/1.0",
"Accept": "application/json"
}
response = requests.get("https://api.example.com/data", headers=headers)
print(response.text)
六、处理 Cookie
Curl 命令:
curl "https://api.example.com/profile" \
-H "Cookie: session_id=abc123; user_token=xyz789"
Python 代码:
import requests
cookies = {
"session_id": "abc123",
"user_token": "xyz789"
}
response = requests.get("https://api.example.com/profile", cookies=cookies)
print(response.text)
七、处理文件上传
Curl 命令:
curl -X POST "https://api.example.com/upload" \
-F "file=@/path/to/file.txt" \
-F "description=测试文件"
Python 代码:
import requests
files = {
"file": open("/path/to/file.txt", "rb")
}
data = {
"description": "测试文件"
}
response = requests.post("https://api.example.com/upload", files=files, data=data)
print(response.text)
# 记得关闭文件
files["file"].close()
八、处理超时和重试
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 设置超时
response = requests.get("https://api.example.com/data", timeout=10)
# 设置重试策略
session = requests.Session()
retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("http://", adapter)
session.mount("https://", adapter)
response = session.get("https://api.example.com/data")
九、使用 EasySpider 在线工具
为了简化转换过程,我们提供了在线 Curl 转 Python 工具:
- 访问 EasySpider 首页
- 选择 "Curl转码" 功能
- 粘贴 Curl 命令
- 自动生成 Python 代码
- 一键复制使用
使用提示:
- 支持所有常见的 Curl 参数
- 自动处理请求头、Cookie、参数等
- 生成的代码可直接运行
- 支持批量转换
十、常见问题
注意事项:
- 确保已安装 requests 库:
pip install requests - 处理 HTTPS 请求时注意证书验证
- 敏感信息(如 API Key)不要硬编码在代码中
- 生产环境建议使用环境变量管理配置
总结
掌握 Curl 转 Python 的技巧可以大大提高开发效率。通过本文的学习,你应该能够:
- 理解 Curl 命令的结构
- 熟练转换各种类型的 HTTP 请求
- 处理复杂的请求参数和认证
- 使用 EasySpider 在线工具快速转换