1,149
社区成员




不过,需要注意的是,有时候官方域名可能会发生变化或者遇到暂时不可访问的情况。在2022年6月1日的记录中提到requests
官网的域名曾经过期,但这通常是暂时的问题,域名持有者通常会及时续费以保证网站的正常运行。
如果上述链接无法访问,你可以尝试通过搜索引擎查找最新的requests
库官网和文档链接,或者访问其GitHub仓库页面,requests
库的GitHub仓库地址是:
在GitHub仓库中,你可以找到项目的最新信息、源代码、问题追踪、贡献指南以及如何报告问题等详细信息。此外,GitHub仓库通常也会链接到最新的文档页面。
requests
是Python中最流行且易于使用的HTTP库之一,它允许你发送HTTP/1.1请求极其简单,同时也保持了灵活性。requests
库支持多种HTTP请求类型,如GET、POST、PUT、DELETE、PATCH、HEAD等,同时提供了处理cookies、重定向、认证、SSL验证等功能。
以下是requests
库的一些主要特性和使用方法:
requests
提供了直观且易于理解的API。发送GET请求
import requests
response = requests.get('http://www.example.com')
print(response.text)
发送POST请求
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('http://www.example.com', data=data)
处理JSON响应
response = requests.get('http://www.example.com/api/data')
data = response.json()
设置headers
headers = {'User-Agent': 'my-app/0.0.1'}
response = requests.get('http://www.example.com', headers=headers)
文件上传
files = {'file': open('report.csv', 'rb')}
response = requests.post('http://www.example.com/upload', files=files)
超时设置
try:
response = requests.get('http://www.example.com', timeout=0.001)
except requests.exceptions.Timeout:
# A timeout occurred
pass
异常处理
try:
response = requests.get('http://www.example.com')
response.raise_for_status() # Raises stored HTTPError, if one occurred.
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)
可以通过pip安装requests
库:
pip install requests
以上是requests
库的基本介绍和一些常见操作的例子,更多高级功能和详细文档可以在其官方网站和文档中找到。