【Python Requests 库详解】

目录

简介

requests 是 Python 中最流行的 HTTP 客户端库,以简洁的 API 设计和强大的功能著称。以下是其核心知识点的详细说明及代码示例。


一、安装与导入

安装

bash 复制代码
# 使用 pip 安装 requests 库
pip install requests

导入

python 复制代码
# 导入 requests 模块
import requests

二、发送 HTTP 请求

1. GET 请求

基本请求
python 复制代码
# 发送一个简单的 GET 请求
response = requests.get("https://api.example.com/data")
print("响应状态码:", response.status_code)
URL 参数
python 复制代码
# 通过 params 参数传递 URL 查询参数
params = {
    "page": 1,          # 当前页码
    "limit": 10,        # 每页数据量
    "sort": "desc"      # 排序方式
}
response = requests.get("https://api.example.com/items", params=params)
print("实际请求的 URL:", response.url)  # 输出构造后的完整 URL

2. POST 请求

表单数据提交
python 复制代码
# 模拟表单提交(Content-Type: application/x-www-form-urlencoded)
form_data = {
    "username": "admin",
    "password": "secret"
}
response = requests.post("https://api.example.com/login", data=form_data)
print("登录响应:", response.text)
JSON 数据提交
python 复制代码
# 发送 JSON 格式数据(Content-Type: application/json)
user_info = {
    "name": "Alice",
    "age": 25,
    "email": "[email protected]"
}
response = requests.post("https://api.example.com/users", json=user_info)
print("创建用户结果:", response.json())
文件上传
python 复制代码
# 上传本地文件(Content-Type: multipart/form-data)
with open("report.pdf", "rb") as file:  # 以二进制模式打开文件
    files = {"document": ("report.pdf", file, "application/pdf")}
    response = requests.post("https://api.example.com/upload", files=files)
print("文件上传结果:", response.json())

3. 其他方法

PUT 请求示例
python 复制代码
# 更新资源(例如修改用户信息)
update_data = {"email": "[email protected]"}
response = requests.put("https://api.example.com/users/123", json=update_data)
print("更新结果状态码:", response.status_code)
DELETE 请求示例
python 复制代码
# 删除资源(例如删除文章)
response = requests.delete("https://api.example.com/articles/456")
print("删除操作状态码:", response.status_code)

三、处理响应

1. 响应内容解析

文本内容处理
python 复制代码
# 获取响应文本(自动解码)
response = requests.get("https://api.example.com/text")
print("文本内容:", response.text[:100])  # 截取前 100 个字符
二进制内容处理
python 复制代码
# 下载图片并保存到本地
response = requests.get("https://example.com/image.jpg")
with open("image.jpg", "wb") as f:  # 以二进制写入模式保存
    f.write(response.content)
print("图片下载完成")
JSON 数据处理
python 复制代码
# 解析 JSON 响应(自动转换为字典)
response = requests.get("https://api.example.com/products/789")
product = response.json()
print(f"产品名称: {product['name']}, 价格: {product['price']}元")

2. 响应状态与头信息

状态码检查
python 复制代码
response = requests.get("https://api.example.com/health")
if response.status_code == 200:
    print("服务正常")
elif response.status_code == 404:
    print("资源不存在")
else:
    print("未知状态:", response.status_code)
异常处理
python 复制代码
# 自动抛出 HTTP 错误
try:
    response = requests.get("https://api.example.com/error")
    response.raise_for_status()  # 如果状态码是 4xx/5xx 会抛出异常
except requests.HTTPError as err:
    print(f"HTTP 错误: {err}")
响应头解析
python 复制代码
response = requests.head("https://example.com")  # HEAD 请求只获取头信息
print("服务器类型:", response.headers.get("Server", "未知"))
print("内容类型:", response.headers.get("Content-Type", "未指定"))

四、高级功能

1. 会话(Session)管理

python 复制代码
# 使用 Session 保持登录状态和连接池
with requests.Session() as session:
    # 第一次请求:登录
    login_data = {"user": "admin", "pass": "123456"}
    session.post("https://api.example.com/login", data=login_data)

    # 后续请求:自动携带 Cookie
    profile = session.get("https://api.example.com/profile").json()
    orders = session.get("https://api.example.com/orders").json()
    print("用户信息:", profile)

2. 代理设置

python 复制代码
# 通过代理服务器发送请求
proxies = {
    "http": "http://10.10.1.10:3128",   # HTTP 代理
    "https": "http://10.10.1.10:1080"   # HTTPS 代理
}
try:
    response = requests.get("http://external-site.com", proxies=proxies, timeout=5)
    print("通过代理访问成功")
except requests.ConnectTimeout:
    print("代理连接超时")

3. 超时控制

python 复制代码
# 设置连接超时和读取超时(元组格式)
try:
    response = requests.get("https://slow-api.example.com", timeout=(3.05, 10))
    # 连接超时 3.05 秒,读取超时 10 秒
except requests.Timeout:
    print("请求超时,请重试")

4. SSL 验证

禁用验证(测试环境使用)
python 复制代码
# 注意:verify=False 会忽略 SSL 证书验证(不安全!)
response = requests.get("https://self-signed-cert-site.com", verify=False)
print("跳过 SSL 验证结果:", response.status_code)
自定义证书
python 复制代码
# 使用自定义 CA 证书验证
response = requests.get("https://internal-api.example.com", 
                       verify="/path/to/corporate-ca-bundle.crt")
print("自定义证书验证成功")

五、异常处理

完整异常处理示例

python 复制代码
from requests.exceptions import RequestException

url = "https://unstable-api.example.com/data"

try:
    # 设置超时和重试
    response = requests.get(url, timeout=5)
    response.raise_for_status()  # 检查 4xx/5xx 错误
    
    # 处理成功响应
    data = response.json()
    print("获取数据成功:", data["key"])

except requests.Timeout:
    print("错误: 请求超时(5 秒)")
except requests.HTTPError as e:
    print(f"HTTP 错误: {e.response.status_code} - {e.response.reason}")
except requests.JSONDecodeError:
    print("错误: 响应不是有效的 JSON")
except RequestException as e:
    print(f"请求失败: {str(e)}")

六、其他实用功能

python 复制代码
# 手动设置 Cookie
cookies = {"session_id": "abc123xyz"}
response = requests.get("https://api.example.com/cart", cookies=cookies)
print("购物车内容:", response.json())

# 获取服务器设置的 Cookie
print("服务器返回的 Cookies:")
for cookie in response.cookies:
    print(f"{cookie.name}: {cookie.value}")

2. 重定向控制

python 复制代码
# 禁用自动重定向
response = requests.get("https://example.com/old-url", allow_redirects=False)
if response.status_code == 301:
    new_location = response.headers["Location"]
    print(f"需要手动跳转到: {new_location}")

3. 流式下载大文件

python 复制代码
# 分块下载大文件(避免内存溢出)
url = "https://example.com/large-video.mp4"
with requests.get(url, stream=True) as r:
    r.raise_for_status()
    with open("video.mp4", "wb") as f:
        for chunk in r.iter_content(chunk_size=8192):  # 8KB 块
            f.write(chunk)
print("大文件下载完成")

4. 身份认证

python 复制代码
# 使用 Basic 认证
from requests.auth import HTTPBasicAuth

response = requests.get("https://api.example.com/secured",
                       auth=HTTPBasicAuth("user", "pass"))
print("认证结果:", response.status_code)

七、最佳实践

推荐做法

  1. 会话复用

    python 复制代码
    # 使用 with 语句自动管理 Session
    with requests.Session() as s:
        s.headers.update({"User-Agent": "MyApp/1.0"})  # 统一设置请求头
        s.get("https://api.example.com/ping")          # 测试连接
  2. 超时必填

    python 复制代码
    # 所有请求都应该设置超时
    requests.get("https://example.com", timeout=(3, 10))  # 连接 3 秒,读取 10 秒
  3. 资源释放

    python 复制代码
    # 使用 with 语句确保响应资源释放
    with requests.get("https://example.com", stream=True) as response:
        # 处理响应内容
        pass
  4. 错误重试

    python 复制代码
    # 实现简单重试逻辑
    retries = 3
    for _ in range(retries):
        try:
            response = requests.get(url, timeout=5)
            response.raise_for_status()
            break
        except (RequestException, Timeout):
            continue

官方文档:https://docs.python-requests.org/


特点说明

  1. 代码注释:每个示例都添加了详细的注释,说明代码用途和关键参数
  2. 场景覆盖:包含常见场景(登录、文件传输、错误处理)和特殊需求(代理、SSL 验证)
  3. 安全提示:在禁用 SSL 验证等危险操作时添加警告说明
  4. 最佳实践:强调超时设置、会话管理和资源释放的重要性

可直接保存为 .md 文件或导入到文档工具中使用。

相关推荐
严文文-Chris3 分钟前
方法区、堆、虚拟机栈、寄存器分别存储哪些内容?为什么存储这些内容?
java·开发语言
Lounger669 分钟前
107.二叉树的层序遍历II- 力扣(LeetCode)
python·算法·leetcode
Code_流苏9 分钟前
《Python星球日记》第25天:Pandas 数据分析
python·数据分析·pandas·数据聚合·时间序列
无名之逆13 分钟前
Hyperlane 文件分块上传服务端
服务器·开发语言·前端·网络·http·rust·php
程序员三藏15 分钟前
Selenium三大等待
自动化测试·软件测试·数据库·python·selenium·测试工具·测试用例
Miraitowa_cheems28 分钟前
JAVA SE 自我总结
java·开发语言·javase
码猩37 分钟前
C# winform根据EXCEL匹配文件后将txt的图片分别下载到指定的文件夹里
开发语言·c#·excel
明月看潮生40 分钟前
青少年编程与数学 02-016 Python数据结构与算法 14课题、动态规划
python·算法·青少年编程·动态规划·编程与数学
明月看潮生41 分钟前
青少年编程与数学 02-016 Python数据结构与算法 11课题、分治
python·算法·青少年编程·编程与数学
Alt.942 分钟前
SpringMVC基础三(json)
java·开发语言