安裝:python
pip install requestsnginx
使用:git
import requests github
HTTP請求:GET、POST、PUT、DELETE、HEAD、OPTIONSjson
1) getapi
res = requests.get("https://github.com/timeline.json") 服務器
2) postcookie
res = requests.post("http://httpbin.org/post");app
3) putpost
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"}}'