Python Requests库
背景
Requests is an elegant and simple HTTP library for Python, built for human beings.
Requests是一个优雅简洁的Python HTTP库,给人类使用。
Requests使用urllib3,所以拥有它的所有特性。
支持python 2.6 – 3.5 。
不得不说,超级喜欢他们家的文档啊,很pythonic。
安装
pip install requests
发送请求
>>> import requests >>> r = requests.get('https://github.com/timeline.json') >>> r = requests.post("http://httpbin.org/post") >>> r = requests.put("http://httpbin.org/put") >>> r = requests.delete("http://httpbin.org/delete") >>> r = requests.head("http://httpbin.org/get") >>> r = requests.options("http://httpbin.org/get")
GET
不带参数的GET:
>>> r = requests.get('https://github.com/timeline.json')
带有参数的GET:
>>> payload = {'key1': 'value1', 'key2': 'value2'} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?key2=value2&key1=value1 >>> 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
带有header的GET:
>>> url = 'https://api.github.com/some/endpoint' >>> headers = {'user-agent': 'my-app/0.0.1'} >>> r = requests.get(url, headers=headers)
设置连接的超时时间:
>>> requests.get('http://github.com', timeout=0.001) Traceback (most recent call last): File "<stdin>", line 1, in <module> requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
带有cookie的GET:
>>> url = 'http://httpbin.org/cookies' >>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies) >>> r.text '{"cookies": {"cookies_are": "working"}}'
POST
不带参数的POST:
>>> r = requests.post("http://httpbin.org/post")
带有参数的POST:
>>> payload = {'key1': 'value1', 'key2': 'value2'} >>> r = requests.post("http://httpbin.org/post", data=payload) >>> print(r.text) { ... "form": { "key2": "value2", "key1": "value1" }, ... }
Response
URL:
>>> import requests >>> r = requests.get('https://github.com/timeline.json') >>>r.url 'https://github.com/timeline.json'
返回的内容:
>>> r.text //以encoding自动解码出来的结果 u'[{"repository":{"open_issues":0,"url":"https://github.com/... >>> r.content //返回的二进制相应内容 b'[{"repository":{"open_issues":0,"url":"https://github.com/... >>> r.raw //返回的原始内容 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810> >>> r.json() //以json形式解析 [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
使用的编码:
>>> r.encoding 'utf-8' >>> r.encoding = 'ISO-8859-1' //设置编码
相应的状态码:
>>> r.status_code
200
>>> r.status_code == requests.codes.ok True
返回的header:
>>> r.headers { 'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'connection': 'close', 'server': 'nginx/1.0.4', 'x-runtime': '148ms', 'etag': '"e1ca502697e5c9317743dc078f67693f"', 'content-type': 'application/json' } >>> r.headers['Content-Type'] 'application/json' >>> r.headers.get('content-type') 'application/json'
COOKIE:
>>> r.cookies['example_cookie_name'] 'example_cookie_value'
作者:微微微笑
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.