requests庫git
發送請求:github
能夠處理全部請求類型:get、post、put、Delete、Head、Options
r = requests.get(''https://httpbin.org/')json
r = requests.post('https://httpbin.org/post')post
r = requests.put('https://httpbin.org/put')編碼
r = requests.delete('https://httpbin.org/delete')url
r = requests.options('https://httpbin.org/options')spa
傳遞參數code
payload = {'key1': 'value1', 'key2': 'value2'}get
r = requests.get("http://httpbin.org/get",params=payload)requests
print(r.url)
相應內容
requests 讀取相應內容
r = requests.get('https://github.com/timeline.json')
r.text
推測文本編碼,找出Requests使用了什麼編碼 並使用r.encoding屬性改變
r.encoding = 'ISO-8859-1'
JSON相應內容
Requests中也內置JSON解碼器
r = requests.get('https://github.com/timeline.json')
r.json()
相應狀態碼
r = requests.get('http://httpbin.org/get')
r.status_code == requests.codes.ok
拋出異常處理
bad_r = requests.get('http://httpbin.org/status/404')
bad_r.raise_for_status()