安装:
pip install requests
使用:
import requests
HTTP请求:GET、POST、PUT、DELETE、HEAD、OPTIONS
1) get
res = requests.get("https://github.com/timeline.json")
2) post
res = requests.post("http://httpbin.org/post");
3) put
res = requests.put("http://httpbin.org/put");
4) delete
res = requests.delete("http://httpbin.org/delete");
5) head
res = requests.head("http://httpbin.org/get") ;
6) options
res = requests.options("http://httpbin.org/get")
为URL传递参数
requests模块使用params关键字参数,以一个字典的形式来提供参数。
>>> payload = {‘key1‘:‘value‘,‘key2‘:‘value2‘}
>>> res = requests.get("http://httpbin.org/get",params=payload)
>>> res.url
u‘http://httpbin.org/get?key2=value2&key1=value‘
查看响应内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.text
u‘{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}‘
requests模块会自动解码来自服务器的内容,可以使用res.encoding来查看编码,在你需要的情况下,request也可以使用定制的编码,并使用codes模块进行注册,这样你就可以轻松的使用这个解码器的名称作为res.encoding的值
>>> res.encoding
‘utf-8‘
>>> res.encoding = "gbk2312"
以json格式获取响应的内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.json()
{u‘documentation_url‘: u‘https://developer.github.com/v3/activity/events/#list-public-events‘, u‘message‘: u‘Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.‘}
原始的应该内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x0000000002F31550>
>>> res.raw.read(10)
‘‘
定制请求头
>>> import json
>>> url = ‘https://api.github.com/some/endpoint‘
>>> payload = {‘some‘: ‘data‘}
>>> headers = {‘content-type‘: ‘application/json‘}
post请求参数是这样:
>>> payload = {‘key1‘:‘value1‘,‘key2‘:‘value2‘}
>>> url = "http://httpbin.org/post"
>>> res =requests.post(url,data=payload)
>>> res.text
u‘{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "23", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.10.0"\n }, \n "json": null, \n "origin": "218.240.129.20", \n "url": "http://httpbin.org/post"\n}\n‘
>>> print res.text
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.10.0"
},
"json": null,
"origin": "218.240.129.20",
"url": "http://httpbin.org/post"
}
响应状态码及响应头:
>>> res = requests.get("http://httpbin.org/get")
>>> res.status_code
200
>>> res.status_code == requests.codes.ok
True
>>> res.headers
{‘Content-Length‘: ‘239‘, ‘Server‘: ‘nginx‘, ‘Connection‘: ‘keep-alive‘, ‘Access-Control-Allow-Credentials‘: ‘true‘, ‘Date‘: ‘Sun, 22 May 2016 09:24:10 GMT‘, ‘Access-Control-Allow-Origin‘: ‘*‘, ‘Content-Type‘: ‘application/json‘}
>>> print res.headers
{‘Content-Length‘: ‘239‘, ‘Server‘: ‘nginx‘, ‘Connection‘: ‘keep-alive‘, ‘Access-Control-Allow-Credentials‘: ‘true‘, ‘Date‘: ‘Sun, 22 May 2016 09:24:10 GMT‘, ‘Access-Control-Allow-Origin‘: ‘*‘, ‘Content-Type‘: ‘application/json‘}
>>> res.headers[‘Content-Type‘]
‘application/json‘
>>> res.headers.get(‘content-type‘)
‘application/json‘
>>> res.headers.get(‘Connection‘)
‘keep-alive‘
>>>
Cookies:
访问cookies
>>> url = ‘http://example.com/some/cookie/setting/url‘
>>> r = requests.get(url)
>>> r.cookies[‘example_cookie_name‘]
设置cookies
>>> url = ‘http://httpbin.org/cookies‘
>>> cookies = dict(cookies_are=‘working‘)
>>> r = requests.get(url, cookies=cookies)
>>> r.text
‘{"cookies": {"cookies_are": "working"}}‘
原文地址:https://www.cnblogs.com/logo-88/p/8367188.html