Requests 是一个简单而优雅的 HTTP 库。
>>> import requests
>>> r = requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"authenticated": true, ...'
>>> r.json()
{'authenticated': True, ...}Requests 可以让你极其轻松地发送 HTTP/1.1 请求。无需手动将查询字符串添加到 URL,也无需对 PUT 和 POST 数据进行表单编码 — 但如今,请直接使用 json 方法!
Requests 是当今下载量最大的 Python 包之一,每周吸引大约 3000 万次下载 — 据 GitHub 统计,Requests 目前被 100 万多个 仓库依赖。你可以完全信任这段代码。
Requests 可在 PyPI 上获取:
$ python -m pip install requestsRequests officially supports Python 3.9+。
Requests 已准备好满足构建健壮可靠的 HTTP 通信应用程序的需求。
- Keep-Alive 和连接池
- 国际域名和 URL
- 带 Cookie 持久化的 Session
- 浏览器式 TLS/SSL 验证
- 基本和摘要认证
- 类似
dict的 Cookie - 自动内容解压和解码
- 多部分文件上传
- SOCKS 代理支持
- 连接超时
- 流式下载
- 自动遵守
.netrc - 分块 HTTP 请求
API 参考和用户指南可在 Read the Docs 上找到
克隆 Requests 仓库时,可能需要添加 -c fetch.fsck.badTimezone=ignore 标志以避免有关错误提交时间戳的错误(有关更多背景信息,请参阅
此问题):
git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git你也可以将此设置应用于全局 Git 配置:
git config --global fetch.fsck.badTimezone ignore

