psf/requests
GitHub: psf/requests
简洁优雅的 Python HTTP 客户端库,让发送和处置 HTTP 请求变得极其简单直觉。
Stars: 54032 | Forks: 9947
# Requests
[](https://pypi.org/project/requests/)
[](https://pypi.org/project/requests)
[](https://pepy.tech/project/requests)
[](https://github.com/psf/requests/graphs/contributors)
[](https://requests.readthedocs.io)
**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 包之一,每周下载量约为 `3 亿次`——根据 GitHub 的数据,目前有超过 `400 万` 个仓库[依赖](https://github.com/psf/requests/network/dependents?package_id=UGFja2FnZS01NzA4OTExNg%3D%3D) Requests。
## 安装 Requests 及支持的版本
Requests 可在 PyPI 上获取:
```
$ python -m pip install requests
```
Requests 官方支持 Python 3.10+。
## 支持的功能与最佳实践
Requests 已经为满足当今构建健壮且可靠的 HTTP 应用的需求做好了准备。
- Keep-Alive 与连接池
- 国际化域名和 URL
- 带有 Cookie 持久化的会话
- 浏览器风格的 TLS/SSL 验证
- Basic 与 Digest 认证
- 类似 `dict` 风格的 Cookies
- 自动内容解压缩和解码
- 多部分文件上传
- SOCKS 代理支持
- 连接超时
- 流式下载
- 自动遵循 `.netrc` 配置
- 分块 HTTP 请求
## 克隆仓库
在克隆 Requests 仓库时,您可能需要添加 `-c
fetch.fsck.badTimezone=ignore` 标志,以避免出现关于错误提交时间戳的报错(有关更多背景信息,请参阅
[此 issue](https://github.com/psf/requests/issues/2690)):
```
git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git
```
您也可以将此设置应用到您的全局 Git 配置中:
```
git config --global fetch.fsck.badTimezone ignore
```
[](https://kennethreitz.org) [](https://www.python.org/psf)
标签:API请求, Cookie管理, DNS解析, HTTPS, Python3, Requests, RESTful, Syscall, URL抓取, Web开发, 开源项目, 数据抓取, 网络爬虫基础, 网络请求库, 认证, 逆向工具