【Requests库】主要方法解析 - 图1

request方法

image.png

method参数

image.png

**kwargs:控制访问参数,均为可选项

  1. params: 字典或字节序列,作为参数增加到url

    1. >>> kv = {'key1': 'value1', 'key2': 'value2'}
    2. >>> r = requests.request('GET', 'http://python123.io/ws', param=kv)
    3. >>> print(r.url)
    4. http://http://python123.io/ws?key1=value1&key2=value2
  2. data:字典、字节序列或文件对象,主要是提交用

    1. >>> kv = {'key1': 'value1', 'key2': 'value2'}
    2. >>> r = requests.request('POST', 'http://python123.io/ws', data=kv)
    3. >>> body = 'main body'
    4. >>> r = requests.request('POST', 'http://python123.io/ws', data=body)
  3. json::JSON格式的数据,作为Request内容提交

    1. >>> kv = {'key1': 'value1', 'key2': 'value2'}
    2. >>> r = requests.request('POST', 'http://python123.io/ws', json=kv)
  4. headers:字典,HTTP定制头

    1. >>> hd = {'user-agent': 'Chrome/10'}
    2. >>> r = requests.request('POST', 'http://python123.io/ws', headers=hd)
  5. cookies:字典或CookieJar,Request中的cookie

  6. auth:元组,支持HTTP认证功能
  7. files:字典类型,传输文件

    1. >>> fs = {'file': open('data.xls', 'rb')}
    2. >>> r = requests.request('POST', 'http://python123.io/ws', files=fs)
  8. timeout:秒为单位

  9. proxies:字典类型,设定访问的代理服务器,可以增加登录认证

    • 隐藏源IP地址信息,有效防止对爬虫的逆追踪
      1. >>> pxs = {'http':'http://user:pass@10.10.10.1:1234',
      2. 'https':"https://10.10.10.1:4321"}
      3. >>> r = requests.request('GET', 'http://www.baidu.com', proxies=pxs)
  10. allow_redirects:True/False,默认True,重定向开关

  11. stream:True/False,默认True,获取内容立即下载开关
  12. verify:True/False,默认True,认证SSL证书开关
  13. cert:本地SSl证书路径

    get()方法

    image.png

    head()方法

    image.png

    post()方法

    image.png

    put()方法

    image.png

    patch() 方法

    image.png

    delete()方法

    image.png