转发:https://www.cnblogs.com/EvileOn/p/5517235.html
python request模块学习
安装:
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关键字参数,以一个字典的形式来提供参数。
>>> res = requests.get("http://httpbin.org/get",params=payload)
>>> res.url
u'http://httpbin.org/get?key2=value2&key1=value'
查看响应内容
>>> res.text
u'{"message":"Hello there, wayfaring stranger. If youu2019re reading this then you probably didnu2019t 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的值
'utf-8'
>>> res.encoding = "gbk2312"
以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 youu2019re reading this then you probably didnu2019t 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.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x0000000002F31550>
>>> res.raw.read(10)
''
定制请求头
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}
post请求参数是这样:
>>> url = "http://httpbin.org/post"
>>> res =requests.post(url,data=payload)
>>> res.text
u'{ "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" } '
>>> 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")
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
>>> r = requests.get(url)
>>> r.cookies['example_cookie_name']
设置cookies
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'