享受每一天,Just Enjoy !

0%

Python requests库(一)

1.Requests 库介绍

  • Requests是一个Python HTTP库,在Apache 2许可证下发布。该项目的目标是使HTTP请求更简单,更人性化。

2.安装方式

  • 直接使用pip安装

    1
    pip install requests
  • 验证是否安装成功,如果可以正常导入,则说明成功

    1
    2
    3
    4
    5
    Python 3.7.5 (default, Nov 29 2019, 14:32:46)
    [Clang 9.0.0 (clang-900.0.39.2)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import requests
    >>>

    3.简单的例子

  • 进入python命令行模式

    1
    2
    3
    4
    5
    6
    ➜  ~ source yiluo/bin/activate
    (yiluo) ➜ ~ python
    Python 3.7.5 (default, Nov 29 2019, 14:32:46)
    [Clang 9.0.0 (clang-900.0.39.2)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  • 请求下简书的首页先

    1
    2
    3
    4
    5
    >>> import requests
    >>> r =requests.get('https://www.jianshu.com/')
    >>> print(r.status_code)
    403
    >>>

    很不幸返回的状态码不是 200

  • 加个header再尝试一下

    1
    2
    3
    4
    5
    >>> import requests
    >>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    >>> r = requests.get('https://www.jianshu.com', headers = headers)
    >>> print(r.status_code)
    200

    这次请求成功了,上面代码中 rResponse对象,可以在这个对象中获取到我们想要的信息, 我们再输出些其他的响应内容看下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> import requests
    >>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    >>> r = requests.get('https://www.jianshu.com', headers = headers)
    >>> print(type(r))
    <class 'requests.models.Response'>
    >>> print(type(r.text))
    <class 'str'>
    >>> print(r.cookies)
    <RequestsCookieJar[<Cookie locale=zh-CN for www.jianshu.com/>]>

    如上输出了响应的类型响应体类型响应的cookies

  • 其他 HTTP 请求类型 PUTDELETEHEAD 以及 OPTIONS

    1
    2
    3
    4
    >>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
    >>> r = requests.delete('http://httpbin.org/delete')
    >>> r = requests.head('http://httpbin.org/get')
    >>> r = requests.options('http://httpbin.org/get')

    4.经常使用的请求

  • requests 构建GET请求的方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    >>> import requests
    >>> r = requests.get('http://httpbin.org/get')
    >>> print(r.text)
    {
    "args": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
    },
    "origin": "116.25.147.198, 116.25.147.198",
    "url": "https://httpbin.org/get"
    }
  • GET请求传递参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    >>> import requests
    >>> data = {'name':'Tom', 'age':18}
    >>> r = requests.get("http://httpbin.org/get", params=data)
    >>> print(r.text)
    {
    "args": {
    "age": "18",
    "name": "Tom"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
    },
    "origin": "116.25.147.198, 116.25.147.198",
    "url": "https://httpbin.org/get?name=Tom&age=18"
    }
  • requests 构建POST请求的方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    >>> import requests
    >>> data = {'name':'Tom', 'age':'18'}
    >>> r = requests.post('http://httpbin.org/post', data = data)
    >>> print(r.text)
    {
    "args": {},
    "data": "",
    "files": {},
    "form": {
    "age": "18",
    "name": "Tom"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "15",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
    },
    "json": null,
    "origin": "116.25.147.198, 116.25.147.198",
    "url": "https://httpbin.org/post"
    }

    5.具体的一些使用方式

  • 传递URL参数
    Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数

    1
    2
    3
    4
    5
    >>> import requests
    >>> payload = {'key1': 'value1', 'key2': 'value2'}
    >>> r = requests.get("http://httpbin.org/get", params=payload)
    >>> print(r.url)
    http://httpbin.org/get?key1=value1&key2=value2
  • JSON 响应内容
    Requests 内置的 JSON 解码器,处理 JSON 数据:

    1
    2
    3
    >>> import requests
    >>> r = requests.get('https://api.github.com/events')
    >>> r.json()
  • 定制请求头
    为请求添加 HTTP 头部,只要传递一个 dictheaders 参数就可以

    1
    2
    3
    4
    >>> import requests
    >>> url = 'https://api.github.com/some/endpoint'
    >>> headers = {'user-agent': 'my-app/0.0.1'}
    >>> r = requests.get(url, headers=headers)

……待续

)在这里插入图片描述

享受每一天,Just Enjoy !

关注公众号查看更多内容