requests库(一)

requests

  • 请求:

        指定timeout关键字参数请求时间

    • get

      • url携带数据:

        1
        2
        3
        4
        5
        >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

        >>> r = requests.get('http://httpbin.org/get', params=payload)
        >>> print(r.url)
        http://httpbin.org/get?key1=value1&key2=value2&key2=value3
      • 请求头

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

        >>> r = requests.get(url, headers=headers)
      • Cookie

        1
        2
        3
        4
        5
        6
        7
        >>> jar = requests.cookies.RequestsCookieJar()
        >>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
        >>> jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
        >>> url = 'http://httpbin.org/cookies'
        >>> r = requests.get(url, cookies=jar)
        >>> r.text
        '{"cookies": {"tasty_cookie": "yum"}}'
    • post

      • 表单形式的数据(字典)

        1
        2
        3
        4
        >> payload = {'key1': 'value1', 'key2': 'value2'}

        >>> r = requests.post("http://httpbin.org/post", data=payload)
        >>> print(r.text)
      • 多个元素使用同一key(元组)

        1
        2
        >>> payload = (('key1', 'value1'), ('key1', 'value2'))
        >>> r = requests.post('http://httpbin.org/post', data=payload)
      • 传输string类型数据(json库)

        1
        2
        3
        4
        5
        6
        >>> import json

        >>> url = 'https://api.github.com/some/endpoint'
        >>> payload = {'some': 'data'}

        >>> r = requests.post(url, data=json.dumps(payload))
      • json数据(json关键字参数)

        1
        >>> r = requests.post(url, json=payload)
      • 传输文件(file关键字参数)

            显式地设置文件名,文件类型和请求头:

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        >>> url = 'http://httpbin.org/post'
        >>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

        >>> r = requests.post(url, files=files)
        >>> r.text
        {
        ...
        "files": {
        "file": "<censored...binary...data>"
        },
        ...
        }

        例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> import requests
>>> r = requests.get('https://api.github.com/repos/requests/requests/git/commits/a050faf084662f3a352dd1a941f2c7c9f886d4ad')

>>> if (r.status_code == requests.codes.ok):
... print r.headers['content-type']
...
application/json; charset=utf-8

# 知道返回类型是json,可用json()
>>> commit_data = r.json()

>>> print commit_data.keys()
[u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']

>>> print commit_data[u'committer']
{u'date': u'2012-05-10T11:10:50-07:00', u'email': u'me@kennethreitz.com', u'name': u'Kenneth Reitz'}

>>> print commit_data[u'message']
makin' history

  • 响应

    • 状态:r.status_code 或 r.raise_for_status()抛异常

    • 内容[列表]:r.text

    • 编码[字符串]:r.encoding(若乱码,设定该值)

    • 二进制响应内容[列表]:r.content

      • 例如用于创建图片
        1
        2
        3
        4
        >>> from PIL import Image
        >>> from io import BytesIO

        >>> i = Image.open(BytesIO(r.content))
    • json内容[列表]:r.json()

      • 注意配合r.status_code确保是正确的json数据
    • 响应头:r.headers;而请求头是 r.request.headers

    • cookie{字典}:

      1
      >>> r.cookies['example_cookie_name']
    • 查看重定向[列表]:r.history

      • 请求时的allow_redirects=False参数可以禁止重定向,默认为True