天天看點

python requests用法總結

python requests用法總結

requests是一個很實用的Python HTTP用戶端庫,編寫爬蟲和測試伺服器響應資料時經常會用到。可以說,Requests 完全滿足如今網絡的需求

本文全部來源于官方文檔:

安裝方式一般采用pip install requests。其它安裝方式參考官方文檔

導入子產品: import requests

一、GET請求

r  = requests.get('http://httpbin.org/get')

傳參

>>> payload = {'key1': 'value1', 'key2': 'value2', 'key3': None}

>>> r = requests.get('http://httpbin.org/get', params=payload)

http://httpbin.org/get?key2=value2&key1=value1

Note that any dictionary key whose value is None will not be added to the URL's query string.

參數也可以傳遞清單

>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

>>> print(r.url)

http://httpbin.org/get?key1=value1&key2=value2&key2=value3

r.text        傳回headers中的編碼解析的結果,可以通過r.encoding = 'gbk'來變更解碼方式

r.content     傳回二進制結果

r.json()      傳回JSON格式,可能抛出異常

r.status_code

r.raw         傳回原始socket respons,需要加參數stream=True

>>> r = requests.get('https://api.github.com/events', stream=True)

>>> r.raw

<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>

>>> r.raw.read(10)

'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

将結果儲存到檔案,利用r.iter_content()

with open(filename, 'wb') as fd:

    for chunk in r.iter_content(chunk_size):

        fd.write(chunk)

傳遞headers

>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)

傳遞cookies

>>> url = 'http://httpbin.org/cookies'

>>> r = requests.get(url, cookies=dict(cookies_are='working'))

>>> r.text

'{"cookies": {"cookies_are": "working"}}'

二、POST請求

傳遞表單

r = requests.post('http://httpbin.org/post', data = {'key':'value'})

通常,你想要發送一些編碼為表單形式的資料—非常像一個HTML表單。 要實作這個,隻需簡單地傳遞一個字典給 data 參數。你的資料字典在送出請求時會自動編碼為表單形式:

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("http://httpbin.org/post", data=payload)

>>> print(r.text)

{

  ...

  "form": {

    "key2": "value2",

    "key1": "value1"

  },

}

很多時候你想要發送的資料并非編碼為表單形式的。如果你傳遞一個 string 而不是一個dict ,那麼資料會被直接釋出出去。

>>> url = 'https://api.github.com/some/endpoint'

>>> payload = {'some': 'data'}

>>> r = requests.post(url, data=json.dumps(payload))

或者

>>> r = requests.post(url, json=payload)

傳遞檔案

url = 'http://httpbin.org/post'

>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)

配置files,filename, content_type and headers

files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

響應

r.heards

r.cookies

跳轉

By default Requests will perform location redirection for all verbs except HEAD.

>>> r = requests.get('http://httpbin.org/cookies/set?k2=v2&k1=v1')

>>> r.url

'http://httpbin.org/cookies'

>>> r.status_code

200

>>> r.history

[<Response [302]>]

If you're using HEAD, you can enable redirection as well:

r = requests.head('http://httpbin.org/cookies/set?k2=v2&k1=v1',allow_redirects=True)

You can tell Requests to stop waiting for a response after a given number of seconds with the timeoutparameter:

requests.get('http://github.com', timeout=0.001)

<a href="http://blog.51cto.com/search/result?q=python+requests%E7%94%A8%E6%B3%95%E6%80%BB%E7%BB%93" target="_blank">python requests用法總結</a>

本文轉自    鵬愛   51CTO部落格,原文連結:http://blog.51cto.com/pengai/1978400