Python的requests软件包详解

时间:2020-12-18 18:11:05

requests是一款Python语言的HTTP类库,便于进行HTTP访问。
1. requests的特性

  • 能够发送HTTP 1.1请求
  • 无需手工为GET方法设置URL的请求参数,无需手工为POST方法设置参数编码
  • 借助于urllib3实现HTTP请求的连接会话缓存
  • 支持Python 2.6, 2.7, 3.3-3.7
2.  requests的安装

pip install requests

3. requests的接口
1) Main interfaces

  • requests.request()
  • requests.head()
  • requests.get()
  • requests.post()
  • requests.put()
  • requests.patch()
  • requests.delete()
2) requests.Session类
import requests
requests.Session()

3) requests.Request类
import requests
requests.Request('GET', 'http://httpbin.org/get')

4) requests.PreparedRequest类
import requests
req = requests.Request('GET', 'http://httpbin.org/get')
preq = req.prepare()

5) requests.Response类

import requests
r = requests.get('https://api.github.com/events')
r.text

6) requests.adapters.BaseAdapter类

7) requests.adapters.HTTPAdapter类

requests提供的使用urllib3的HTTP Adapter


参考链接:

文档,http://docs.python-requests.org/en/master/

代码,https://github.com/requests/requests/

Release notes,http://docs.python-requests.org/en/master/community/updates/