[关闭]
@zhengyuhong 2016-03-21T14:03:56.000000Z 字数 3114 阅读 1311

requests

requests python


requests.get

  1. import requests
  2. r = requests.get('https://github.com/timeline.json') # 返回Response对象
  3. payload = {'key1': 'value1', 'key2': 'value2'}
  4. r = requests.get('https://github.com/timeline.json', params=payload)

requests.put

  1. r = requests.put("http://httpbin.org/put")

requests.post

  1. response = requests.post("http://httpbin.org/post")

定制请求头

  1. import json
  2. url = 'https://api.github.com/some/endpoint'
  3. payload = {'some': 'data'}
  4. headers = {'content-type': 'application/json'}
  5. r = requests.post(url, data=json.dumps(payload), headers=headers)

定制参数

  1. payload = {'key1': 'value1', 'key2': 'value2'}
  2. r = requests.post("http://httpbin.org/post", data=payload)
  3. #r = requests.post(url, data=json.dumps(payload), headers=headers)
  4. print r.text
  5. """
  6. {
  7. ...
  8. "form": {
  9. "key2": "value2",
  10. "key1": "value1"
  11. },
  12. ...
  13. }
  14. """

Response.text

读取响应的内容。再次以Github时间线为例:

  1. r = requests.get('https://github.com/timeline.json')
  2. print r.text
  3. # '[{"repository":{"open_issues":0,"url":"https://github.com/...

requests.Session

  1. #-*- coding:utf-8 -*-
  2. import requests
  3. s = requests.Session()
  4. url1 = 'http://www.exanple.com/login'#登陆地址
  5. url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址
  6. data={"user":"user","password":"pass"}
  7. headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
  8. "Accept-Encoding":"gzip",
  9. "Accept-Language":"zh-CN,zh;q=0.8",
  10. "Referer":"http://www.example.com/",
  11. "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
  12. }
  13. res1 = s.post(url1, data=data)
  14. res2 = s.post(url2)
  15. print(resp2.content)

requests.Session.get

  1. get_adapter(url)
  2. Returns the appropriate connnection adapter for the given URL.

requests.Session.post

  1. post(url, data=None, **kwargs)
  2. Sends a POST request. Returns Response object.
  3. 参数:
  4. url URL for the new Request object.
  5. data (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
  6. **kwargs Optional arguments that request takes.

Response.content

以字节的方式访问请求响应体,对于非文本请求,如获取图片:

  1. r = requests.get('https://github.com/timeline.json')
  2. print r.content
  3. # b'[{"repository":{"open_issues":0,"url":"https://github.com/...

Response.json

Requests中也有一个内置的JSON解码器,助你处理JSON数据:

  1. r = requests.get('https://github.com/timeline.json')
  2. print r.json()
  3. # [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

Response.url

  1. import requests
  2. r = requests.get('https://github.com/timeline.json') # 返回Response对象
  3. print r.url
  4. payload = {'key1': 'value1', 'key2': 'value2'}
  5. r = requests.get('https://github.com/timeline.json', params=payload)
  6. print r.url

Response.status_code

检测响应状态码

  1. r = requests.get('http://httpbin.org/get')
  2. r.status_code
  3. # 200

为方便引用,Requests还附带了一个内置的状态码查询对象:

  1. r.status_code == requests.codes.ok
  2. True

Response.headers

查看以一个Python字典形式展示的服务器响应头:

  1. r = requests.get('http://httpbin.org/get')
  2. print r.headers

但是这个字典比较特殊:它是仅为HTTP头部而生的。根据 RFC 2616 , HTTP头部是大小写不敏感的。
因此,我们可以使用任意大写形式来访问这些响应头字段:

  1. r.headers['Content-Type']
  2. # 'application/json; charset=utf-8'
  3. r.headers.get('content-type')
  4. # 'application/json; charset=utf-8'

Response.cookies

检测收到的cookies

  1. url = 'http://example.com/some/cookie/setting/url'
  2. r = requests.get(url)
  3. r.cookies['example_cookie_name']
  4. # 'example_cookie_value'

发送定制cookies

  1. url = 'http://httpbin.org/cookies'
  2. cookies = dict(cookies_are='working')
  3. r = requests.get(url, cookies=cookies)
  4. print r.text
  5. # '{"cookies": {"cookies_are": "working"}}'

快速开始帮助文档
高级用法帮助文档
本文草稿地址

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注