requests HTTP 库
GET/POST 请求、headers/cookies、Session 会话、超时重试
requests HTTP 库
requests 是 Python 最流行的第三方库之一——HTTP 请求从未如此简单。
学完本章你将: 掌握 GET/POST、headers/cookies、Session、超时重试。
GET / POST 请求
python
import requests
# GET
resp = requests.get("https://httpbin.org/get", params={"q": "python"})
print(resp.status_code) # 200
print(resp.json()) # 解析 JSON
print(resp.text) # 原始文本
# POST
resp = requests.post(
"https://httpbin.org/post",
json={"name": "小明", "age": 25},
headers={"Authorization": "Bearer xxx"},
)
print(resp.json())
处理响应
python
resp = requests.get("https://httpbin.org/json")
resp.status_code # 200
resp.ok # True(status < 400)
resp.raise_for_status() # 非 2xx 时抛异常
resp.headers # 响应头
resp.cookies # Cookie
resp.encoding # 编码(自动检测)
resp.elapsed # 请求耗时
Session —— 复用连接 + 保持 Cookie
python
s = requests.Session()
s.headers.update({"User-Agent": "MyApp/1.0"})
s.auth = ("username", "password") # Basic Auth
# 后续请求自动带上 headers/cookies
resp1 = s.get("https://httpbin.org/cookies/set/name/xiaoming")
resp2 = s.get("https://httpbin.org/cookies")
print(resp2.json()) # {"name": "xiaoming"}
超时与重试
python
# 设置超时
resp = requests.get("https://slow.example.com", timeout=5)
# 自动重试
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
s = requests.Session()
retries = Retry(total=3, backoff_factor=0.5)
s.mount("https://", HTTPAdapter(max_retries=retries))