python request模組學習

來源:互聯網
上載者:User

標籤:val   git   als   you   too   obj   name   請求   dev   

安裝:

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")

 

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"}}‘ 

python request模組學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.