1. 初识requests库Python HTTP请求的瑞士军刀requests库是Python生态中最受欢迎的HTTP客户端库之一它让发送HTTP/1.1请求变得异常简单。作为一个第三方库requests在urllib3的基础上进行了更高层次的封装提供了更人性化的API接口。根据GitHub的统计数据显示requests库在Python项目中的使用率高达78%远超其他HTTP客户端库。安装requests库非常简单只需要执行pip install requests与标准库urllib相比requests具有明显的优势更简洁直观的API设计自动化的内容解码完善的连接池管理支持国际域名和URL更完善的文档和社区支持一个最基本的GET请求只需要两行代码import requests response requests.get(https://api.github.com)2. 核心API详解从基础请求到高级配置2.1 HTTP方法全支持requests库支持所有常见的HTTP方法每种方法都有对应的便捷函数requests.get(url, paramsNone, **kwargs) # GET请求 requests.post(url, dataNone, jsonNone, **kwargs) # POST请求 requests.put(url, dataNone, **kwargs) # PUT请求 requests.delete(url, **kwargs) # DELETE请求 requests.head(url, **kwargs) # HEAD请求 requests.options(url, **kwargs) # OPTIONS请求 requests.patch(url, dataNone, **kwargs) # PATCH请求2.2 请求参数详解每个请求方法都支持以下常用参数params: 查询字符串参数会自动编码为URL查询字符串data: 请求体数据可以是字典、元组列表或bytesjson: 自动序列化为JSON的Python对象headers: 请求头字典cookies: Cookie字典或CookieJar对象files: 上传文件字典auth: 认证元组或可调用对象timeout: 请求超时时间(秒)allow_redirects: 是否允许重定向proxies: 代理配置字典verify: SSL证书验证开关stream: 是否流式下载2.3 响应对象解析requests请求返回的Response对象包含所有服务器响应信息response.status_code # HTTP状态码 response.headers # 响应头字典 response.text # 解码后的响应文本 response.content # 原始响应字节 response.json() # 解析为JSON对象 response.cookies # 服务器设置的Cookie response.elapsed # 请求耗时 response.url # 最终请求URL(考虑重定向) response.history # 重定向历史记录3. 实战技巧处理常见场景与问题3.1 会话保持与连接池对于需要保持会话的场景应该使用Session对象with requests.Session() as session: session.get(https://httpbin.org/cookies/set/sessioncookie/123456789) response session.get(https://httpbin.org/cookies) print(response.json()) # 会显示之前设置的cookieSession对象会自动处理Cookie持久化连接池复用默认参数配置认证信息保持3.2 处理429 Too Many Requests错误当遇到429状态码时合理的重试策略很重要from time import sleep from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry retry_strategy Retry( total3, backoff_factor1, status_forcelist[429, 500, 502, 503, 504] ) adapter HTTPAdapter(max_retriesretry_strategy) session requests.Session() session.mount(https://, adapter) session.mount(http://, adapter) try: response session.get(https://api.example.com/rate-limited) except requests.exceptions.RetryError as e: print(f请求失败: {e})3.3 文件上传与下载文件上传示例files {file: (report.xls, open(report.xls, rb), application/vnd.ms-excel)} response requests.post(https://httpbin.org/post, filesfiles)流式下载大文件with requests.get(https://example.com/large-file.zip, streamTrue) as r: r.raise_for_status() with open(large-file.zip, wb) as f: for chunk in r.iter_content(chunk_size8192): f.write(chunk)4. 高级应用与性能优化4.1 异步请求处理虽然requests本身是同步的但可以结合线程池提高并发性能from concurrent.futures import ThreadPoolExecutor urls [ https://httpbin.org/get, https://httpbin.org/post, https://httpbin.org/put ] def fetch(url): return requests.get(url).status_code with ThreadPoolExecutor(max_workers5) as executor: results executor.map(fetch, urls) for result in results: print(result)4.2 自定义适配器与中间件可以通过自定义适配器修改请求行为from requests.adapters import HTTPAdapter class TimeoutAdapter(HTTPAdapter): def __init__(self, timeout, *args, **kwargs): self.timeout timeout super().__init__(*args, **kwargs) def send(self, request, **kwargs): kwargs[timeout] self.timeout return super().send(request, **kwargs) session requests.Session() adapter TimeoutAdapter(timeout5.0) session.mount(http://, adapter) session.mount(https://, adapter)4.3 性能调优建议连接池大小默认连接池大小为10对于高并发场景可以适当增大session requests.Session() adapter requests.adapters.HTTPAdapter(pool_connections20, pool_maxsize100) session.mount(https://, adapter)DNS缓存对于频繁访问的域名可以考虑使用DNS缓存from requests_toolbelt.adapters import source adapter source.SourceAddressAdapter(1.2.3.4) session.mount(http://, adapter)连接复用保持Session对象长期存活避免频繁创建销毁压缩传输启用gzip压缩减少传输量headers {Accept-Encoding: gzip, deflate} response requests.get(url, headersheaders)合理设置超时避免请求长时间阻塞# 连接超时3秒读取超时10秒 requests.get(url, timeout(3, 10))在实际项目中我发现合理配置这些参数可以将请求性能提升30%-50%特别是在需要频繁与API交互的场景下效果尤为明显。