【Python】requests库的介绍及用法

目录

1、应用场景

2、requests-三方库


1、应用场景

Python中的requests库被广泛应用在需要发送HTTP请求的场景中。以下列举了一些主要的应用场景:

  1. API调用: 许多服务提供了API接口,我们可以使用requests库发送GET、POST、PUT、DELETE等请求来获取或修改服务器上的资源,如许多云服务平台的SDK就是基于requests库封装的。
  2. 网页爬虫: requests库常常配合BeautifulSoup等其他库一起用于爬虫。你可以使用requests库获取网页的内容,然后使用BeautifulSoup等库对获取的网页内容进行解析和提取。
  3. 数据下载: 例如,你想从某个网址下载一个文件,可以使用requests库完成这个任务。
  4. 自动化测试: requests库也经常被用在Web应用的自动化测试中,例如模拟用户行为,测试API接口等。
  5. Session管理: requests库可以对cookies进行手动处理,也提供了一个Session对象自动管理和持久化cookies,非常适合登录验证的场景。

总的来说,只要是涉及到HTTP请求,不管是获取信息,还是提交信息,requests库都是非常好用的工具。

2、requests-三方库

Python中requests库的基础使用方法很简单,主要涉及到的HTTP方法包括GET、POST、PUT、DELETE等。以下是一些基础的使用方法:

  • 发送GET请求:

    import requests
    response = requests.get('http://httpbin.org/get')
    print(response.text)

  • 发送POST请求:

    import requests
    data = {'key1': 'value1', 'key2': 'value2'}
    response = requests.post('http://httpbin.org/post', data=data)
    print(response.text)

  • 发送带参数的GET请求:

    import requests
    payload = {'key1': 'value1', 'key2': 'value2'}
    response = requests.get('http://httpbin.org/get', params=payload)
    print(response.url)
    print(response.text)

  • 上传文件:

    import requests
    url = 'http://httpbin.org/post'
    files = {'file': open('report.xls', 'rb')}
    response = requests.post(url, files=files)
    print(response.text)

上述示例中,每个请求返回一个 Response 对象。这个对象包含服务器返回的所有信息,包括响应内容、响应状态码、响应头等。例如:

复制代码
import requests
response = requests.get('http://httpbin.org/get')
print(response.status_code)  # 输出响应状态码
print(response.headers)  # 输出响应头
print(response.cookies)  # 输出cookies
print(response.json())  # 将响应内容解析为JSON

这只是requests库最基础的使用方法,它还有许多高级功能,例如处理Cookies、维护Session、处理重定向等。具体的使用方法可以参考官方文档或者进阶教程。

GET、POST、PUT、DELETE等方法都是HTTP协议中的请求方法,这些方法在requests库中对应相同名字的函数。它们的主要参数如下:

  • url: 请求的URL地址,为字符串。
  • params: 附加到URL中的查询参数,字典或字节序列,可选。
  • data: 请求体中的数据,字典、列表元组、字节序列或文件对象,可选。
  • json: JSON格式的请求体数据,可选。
  • headers: 请求头,为字典格式,可选。
  • cookies: 请求所需要的cookie,字典或CookieJar,可选。
  • files: 要上传的文件,字典类型,例如{'file': open('report.xls', 'rb')},可选。

以下是对各个参数的使用示例:

  • url: 请求的URL地址。

    response = requests.get('http://httpbin.org/get')

  • params: 附加到URL中的查询参数,字典或字节序列,可选。

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

  • data: 请求体中的数据,字典、列表元组、字节序列或文件对象,可选。

    data = {'key1': 'value1', 'key2': 'value2'}
    response = requests.post('http://httpbin.org/post', data=data)

  • json: JSON格式的请求体数据,可选。

    data = {'key1': 'value1', 'key2': 'value2'}
    response = requests.post('http://httpbin.org/post', json=data)

  • headers: 请求头,为字典格式,可选。

    headers = {'user-agent': 'my-app/0.0.1'}
    response = requests.get('http://httpbin.org/get', headers=headers)

  • cookies: 请求所需要的cookie,字典或CookieJar,可选。

    cookies = dict(cookies_are='working')
    response = requests.get('http://httpbin.org/cookies', cookies=cookies)

  • files: 要上传的文件,字典类型,例如{'file': open('report.xls', 'rb')},可选。

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

需要注意的是,以上参数取决于实际HTTP请求的需求和API接口的要求,所以并非所有请求都需要全部使用这些参数,比如一般获取网页内容的GET请求,可能只需要提供URL一个参数即可。

相关推荐
扛麻袋的少年16 分钟前
7.Kotlin的日期类
开发语言·微信·kotlin
麻辣清汤30 分钟前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
钢铁男儿40 分钟前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
钢铁男儿1 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式
野生技术架构师1 小时前
2025年中高级后端开发Java岗八股文最新开源
java·开发语言
前端小趴菜051 小时前
python - range
python
☺����1 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码1
人工智能·python·音视频
静若繁花_jingjing1 小时前
JVM常量池
java·开发语言·jvm
前端小趴菜052 小时前
python - 元组常用操作
python
前端小趴菜052 小时前
python - 列表方法
python