python的requests模块的使用

时间:2022-05-03 22:22:18

安装 Requests

pip install requests

  • 常用的几种用法

get 仅仅获取资源的信息,不增加或者修改数据。
post 一般丢该服务器上的资源,一般我们通过form表单进行提交请求
put 增加
delete 删除

发送请求

  • 首先导入requests模块
    import requests
  • 然后,获取百度网页
    r = requests.get('http://www.baidu.com')
  • 我们使用r来获取想要的信息
  • 其他的请求方式
>>> r = requests.post('http://www.baidu.com', data = {'key':'value'})
>>> r = requests.put('http://www.baidu.com/put', data = {'key':'value'})
>>> r = requests.delete('http://www.baidu.com/delete')
  • url参数的方式请求
  • get的带参数方式
In [6]: pp = {'key1': 'value1', 'key2': 'value2'}

In [7]: r = requests.get("http://www.baidu.com", params=pp)

In [8]: print(r.url)
http://www.baidu.com/?key1=value1&key2=value2
  • post发送表单形式的数据
In [11]: payload = {'key1': 'value1', 'key2': 'value2'}

In [12]: r = requests.post("http://httpbin.org/post", data=payload)

In [13]: print(r.text)
{"args":{},"data":"","files":{},"form":{"key1":"value1","key2":"value2"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"23","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"python-requests/2.19.0"},"json":null,"origin":"221.224.134.178","url":"http://httpbin.org/post"}
  • 响应内容
>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...
In [17]: print(r.encoding)
utf-8
  • json响应内容
>>> import requests

>>> r = requests.get('https://api.github.com/events')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
  • 定制请求头
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)
>>> print(r.headers)
{'Date': 'Wed, 13 Jun 2018 11:54:58 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Server': 'GitHub.com', 'Status': '404 Not Found', 'X-RateLimit-Limit': '60', 'X-RateLimit-Remaining': '58', 'X-RateLimit-Reset': '1528894338', 'X-GitHub-Media-Type': 'github.v3; format=json', 'Access-Control-Expose-Headers': 'ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', 'Access-Control-Allow-Origin': '*', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload', 'X-Frame-Options': 'deny', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Referrer-Policy': 'origin-when-cross-origin, strict-origin-when-cross-origin', 'Content-Security-Policy': "default-src 'none'", 'X-Runtime-rack': '0.010600', 'Content-Encoding': 'gzip', 'X-GitHub-Request-Id': '690A:5BEF:D89B1:11B858:5B210612'}
  • 响应状态码
>>> r = requests.get('http://httpbin.org/get')
>>> r.status_code
200
  • Cookie
    Cookie的常用属性:
    Cookie常用的一些属性:

    1. Domain 域
    2. Path 路径
    3. Expires 过期时间
    4. name 对应的key值
    5. value key对应的value值
  • 如果某个响应中包含一些cookie,你可以快速访问它们

>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)

>>> r.cookies['example_cookie_name']
'example_cookie_value'
  • 要想发送你的cookies到服务器,可以使用 cookies 参数:
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')

>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'
  • requests显示中文的问题,只需要修改编码
In [22]: import requests

In [23]: param={"key1":"hello","key2":"world"}

In [24]: url='https://www.baidu.com/'

In [25]: r=requests.get(url=url)

In [26]: print(r.encoding)
ISO-8859-1

In [27]: r.encoding="utf-8"

In [28]: print(r.encoding)
utf-8