编程 Requests库详细介绍

2024-11-18 05:53:37 +0800 CST views 652

Requests库详细介绍

安装

使用pip安装requests库:

pip install requests

基本用法

requests库提供了简洁的API来处理HTTP请求。以下是使用requests库进行不同HTTP请求的示例:

import requests

# 发送GET请求
r = requests.get('https://api.github.com/events')

# 发送POST请求
r = requests.post('https://httpbin.org/post', data={'key': 'value'})

# 发送PUT请求
r = requests.put('https://httpbin.org/put', data={'key': 'value'})

# 发送DELETE请求
r = requests.delete('https://httpbin.org/delete')

# 发送HEAD请求
r = requests.head('https://httpbin.org/get')

# 发送OPTIONS请求
r = requests.options('https://httpbin.org/get')

在URL中传递参数

可以通过params参数将查询参数附加到URL中:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('https://httpbin.org/get', params=payload)

# 输出拼接后的URL
print(r.url)
# https://httpbin.org/get?key1=value1&key2=value2

响应内容

获取服务响应内容:

import requests

r = requests.get('https://api.github.com/events')
print(r.text)  # 输出响应内容(JSON格式)

可以更改响应的编码:

r.encoding = 'ISO-8859-1'

二进制响应内容

如果响应内容是二进制数据,可以使用r.content获取:

r.content  # 返回二进制内容

JSON响应内容

直接将响应解析为JSON:

import requests

r = requests.get('https://api.github.com/events')
r.json()  # 返回JSON格式的Python对象

自定义请求头

可以通过headers参数设置自定义请求头:

url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}

r = requests.get(url, headers=headers)

POST请求传递表单数据

使用data参数传递表单数据:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post('https://httpbin.org/post', data=payload)

传递文件

上传文件:

url = 'https://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}

r = requests.post(url, files=files)

响应状态代码

可以通过r.status_code获取HTTP状态码:

r = requests.get('https://httpbin.org/get')
print(r.status_code)  # 返回状态码,如200

处理Cookies

访问响应中的Cookies:

url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)

r.cookies['example_cookie_name']

可以通过cookies参数向服务器发送自定义Cookies:

url = 'https://httpbin.org/cookies'
cookies = {'cookies_are': 'working'}

r = requests.get(url, cookies=cookies)
print(r.text)

超时

可以通过timeout参数设置请求超时时间:

requests.get('https://github.com/', timeout=0.001)

Session对象

Session对象允许在多个请求之间持久化参数(如Cookie),并提高性能。

s = requests.Session()
s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('https://httpbin.org/cookies')

print(r.text)  # 显示会话中的Cookies

代理

使用代理发送请求:

proxies = {
    'http': 'http://10.10.1.10:3128',
    'https': 'http://10.10.1.10:1080',
}

r = requests.get('http://example.org', proxies=proxies)

可以为整个会话配置代理:

s = requests.Session()
s.proxies.update(proxies)
s.get('http://example.org')

流式请求

对于大文件或流式API,可以使用stream=True参数推迟下载响应内容:

r = requests.get('https://httpbin.org/stream/20', stream=True)
for line in r.iter_lines():
    if line:
        print(line)

SSL证书验证

默认情况下,requests会验证HTTPS请求的SSL证书。如果需要忽略SSL证书,可以设置verify=False

requests.get('https://kennethreitz.org', verify=False)

或者使用自定义的证书文件:

requests.get('https://github.com', verify='/path/to/certfile')

总结

requests库提供了一个简单易用的接口来处理HTTP请求。它的功能涵盖了基本的HTTP请求、文件上传、Cookie处理、SSL证书验证等多种场景,是Python开发中必备的工具之一。

复制全文 生成海报 编程 网络 Python库

推荐文章

rangeSlider进度条滑块
2024-11-19 06:49:50 +0800 CST
CSS 中的 `scrollbar-width` 属性
2024-11-19 01:32:55 +0800 CST
html5在客户端存储数据
2024-11-17 05:02:17 +0800 CST
Rust开发笔记 | Rust的交互式Shell
2024-11-18 19:55:44 +0800 CST
2024年公司官方网站建设费用解析
2024-11-18 20:21:19 +0800 CST
php使用文件锁解决少量并发问题
2024-11-17 05:07:57 +0800 CST
Vue3中如何处理跨域请求?
2024-11-19 08:43:14 +0800 CST
php机器学习神经网络库
2024-11-19 09:03:47 +0800 CST
mysql时间对比
2024-11-18 14:35:19 +0800 CST
使用 sync.Pool 优化 Go 程序性能
2024-11-19 05:56:51 +0800 CST
前端代码规范 - Commit 提交规范
2024-11-18 10:18:08 +0800 CST
Vue3结合Driver.js实现新手指引功能
2024-11-19 08:46:50 +0800 CST
Nginx rewrite 的用法
2024-11-18 22:59:02 +0800 CST
mysql删除重复数据
2024-11-19 03:19:52 +0800 CST
JavaScript设计模式:装饰器模式
2024-11-19 06:05:51 +0800 CST
程序员茄子在线接单