本文共 2973 字,大约阅读时间需要 9 分钟。
requests库为Python提供了一个强大的HTTP客户端,简化了对RESTful APIs和其他HTTP服务的访问。它的优势在于易用性和Pythonic风格,适合开发者快速完成HTTP请求任务。
• requests不是标准库,需要单独安装
• 最佳的HTTP客户端库,提供Pythonic风格的API • 支持分块下载和自动处理重定向通过pip安装:pip install requests
创建并发送请求,返回Response对象。
参数:
返回:
示例:
import requests response = requests.request('GET', 'http://httpbin.org/get')
发送GET请求,常用于检索数据。
参数包括:
- url:目标URL - params:查询参数字典 - **kwargs:其他请求参数 response = requests.get('http://httpbin.org/get', params={'qs1': 'value1', 'qs2': 'value2'})
发送POST请求,常用于提交数据。
参数包括:
- url:目标URL - data:字典形式的请求体 - json:JSON数据形式的请求体 - **kwargs:其他请求参数 response = requests.post('http://httpbin.org/post', data={'name': 'value'})
发送HEAD请求,用于获取HTTP头信息。
response = requests.head('http://httpbin.org/head')
发送PUT请求,用于更新资源。
response = requests.put('http://httpbin.org/put', data={'name': 'value'})
requests响应对象包含以下信息:
- status_code:HTTP状态码 - headers:HTTP响应头 - json:解析后的JSON数据 - text:Unicode编码的文本内容 - content:原始字节流数据 - cookies:响应中的Cookie信息以下是一些常见的请求示例:
1. 获取GitHub事件列表:
import requests response = requests.get('https://api.github.com/events') print(response.status_code) print(response.json())
2. 发送带查询参数的GET请求:
url = 'http://httpbin.org/get' params = {'qs1': 'value1', 'qs2': 'value2'} response = requests.get(url, params=params) print(response.status_code) print(response.text)
3. 自定义HTTP头:
headers = {'User-Agent': 'Chrome'} response = requests.get('http://httpbin.org/get', headers=headers) print(response.status_code) print(response.headers)
4. 获取Douban的Cookie:
url = 'http://www.douban.com' headers = {'User-Agent': 'Chrome'} response = requests.get(url, headers=headers) print(response.status_code) print(response.cookies['bid'])
1. 使用Session对象:
Session对象可以保持会话参数,提高请求性能和效率。
session = requests.Session() response = session.get('http://httpbin.org/get', params={'qs1': 'value1'}) response2 = session.get('http://httpbin.org/post', data={'name': 'value'})
2. SSL证书管理:
可以通过参数设置是否验证证书,或者自定义CA证书路径。
# 禁用证书验证 response = requests.get('https://api.example.com', verify=False)
3. 文件上传:
支持上传普通文件和复杂结构的文件,例如:
files = {'file': open('local_file.txt', 'rb')} response = requests.post('http://httpbin.org/post', files=files)
4. 代理访问:
可以通过proxies参数配置代理服务器。
proxies = { 'http': 'http://proxy.example.com:3128', 'https': 'http://proxy.example.com:1080' } response = requests.get('http://www.example.com', proxies=proxies)
requests库为Python开发者提供了一个强大而灵活的HTTP客户端工具,适用于从简单的GET请求到复杂的文件上传和代理访问等多种场景。通过合理使用requests的特性,可以显著提升开发效率和代码质量。
转载地址:http://zyqbz.baihongyu.com/