这是专栏优秀的第三方库的第4篇原创文章。
大家好,这里是程序员晚枫,正在all in AI编程实战。
requests 是一个非常流行的 Python HTTP 库,用于发送各种 HTTP 请求。以下是 requests 的一些基本用法:
安装
首先,确保你已经安装了 requests 库。如果没有安装,可以通过以下命令安装:
发送 GET 请求
1 2 3 4 5 6 7 8 9 10 11
| import requests
response = requests.get('https://python-office.com/data')
if response.status_code == 200: data = response.json() print(data) else: print('请求失败,状态码:', response.status_code)
|
发送 POST 请求
1 2 3 4 5 6 7 8 9 10 11 12
| import requests
payload = {'key1': 'value1', 'key2': 'value2'} response = requests.post('https://python-office.com/submit', data=payload)
if response.status_code == 200: result = response.json() print(result) else: print('请求失败,状态码:', response.status_code)
|
1 2 3 4 5 6 7 8 9 10 11 12
| import requests
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' }
response = requests.get('https://python-office.com/data', headers=headers)
if response.status_code == 200: print(response.text) else: print('请求失败,状态码:', response.status_code)
|
发送带有 Cookies 的请求
1 2 3 4 5 6 7 8 9 10 11 12 13
| import requests
cookies = { 'session_id': '123456789', 'token': 'abcdefg' }
response = requests.get('https://python-office.com/data', cookies=cookies)
if response.status_code == 200: print(response.text) else: print('请求失败,状态码:', response.status_code)
|
发送带有认证信息的请求
1 2 3 4 5 6 7 8 9 10
| import requests
auth = ('username', 'password')
response = requests.get('https://python-office.com/protected', auth=auth)
if response.status_code == 200: print(response.text) else: print('请求失败,状态码:', response.status_code)
|
发送文件
1 2 3 4 5 6 7 8 9 10
| import requests
files = {'file': open('report.xls', 'rb')}
response = requests.post('https://python-office.com/upload', files=files)
if response.status_code == 200: print('文件上传成功') else: print('文件上传失败,状态码:', response.status_code)
|
异常处理
1 2 3 4 5 6 7 8 9 10 11
| import requests from requests.exceptions import HTTPError
try: response = requests.get('https://python-office.com/data') response.raise_for_status() print(response.text) except HTTPError as http_err: print(f'HTTP error occurred: {http_err}') except Exception as err: print(f'Other error occurred: {err}')
|
这些是 requests 库的一些基本用法。通过这些示例,你可以了解如何使用 requests 发送不同类型的 HTTP 请求,并处理响应和异常。



相关阅读
程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!