Python模塊-requests(一)
阿新 • • 發佈:2018-02-04
pub host 打印 agen github param hub clas api
requests不是python自帶的,使用前需要安裝
-
發送請求
HTTP請求類型有GET,POST,PUT,DELETE,HEAD和OPTIONS
使用requests發送請求的方法如下:
>>> import requests
>>> r = requests.get("http://httpbin.org/get") #發送GET請求
>>> r = requests.post("http://httpbin.org/post") #發送POST請求
>>> r = requests.put("http://httpbin.org/put") #發送PUT請求
>>> r = requests.delete("http://httpbin.org/delete") #發送DELETE請求
>>> r = requests.head("http://httpbin.org/get") #發送HEAD請求
>>> r = requests.options("http://httpbin.org/get") #發送OPTIONS請求
-
傳遞URL參數
params參數會對傳入的參數進行拼接處理
通常使用params傳的參數為字典的格式
>>> import requests >>> payload = {"word":"test","page":11} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) #打印r裏的url參數的值 http://httpbin.org/get?word=test&page=11
字典裏的值還可以是列表
>>> payload = {"word":"test","page":[1,2,3]} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test&page=1&page=2&page=3
字典中的值為None的鍵將不會被傳參數到url裏
>>> payload = {"word":"test","page":None} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test
params傳的參數也可以直接是字符串
>>> payload = "word=test&page=11" >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test&page=11
-
響應內容
requests能讀取服務器響應的內容
>>> r = requests.get("https://www.cnblogs.com/") >>> r.text #獲取網頁源代碼 ‘‘‘此處為網頁源代碼‘‘‘ >>> r.encoding #查看網頁源代碼的編碼 ‘utf-8‘ >>> r.encoding = ‘GBK‘ #把網頁源碼的編碼改為gbk >>> r.encoding #再調用的時候,發現網頁編碼變成了gbk了 ‘GBK‘
-
二進制響應內容
對於非文本請求,requests也能用字節的方式來訪問請求響應體
>>> r = requests.get("http://p1.ifengimg.com/a/2018_06/75880eeacd0823d_size11_w230_h152.jpg") >>> r.content ‘‘‘此處為bytes類型的圖片內容‘‘‘ >>> r.text ‘‘‘一堆亂碼‘‘‘
該方式也能用於文本請求,不過返回的結果為bytes類型
>>> r = requests.get("https://www.cnblogs.com/") >>> r.text ‘‘‘此處為文本類型的網頁源代碼‘‘‘ >>> r.content ‘‘‘此處為bytes類型的網頁源代碼‘‘‘
-
JSON響應內容
requests中也有一個內置的json解碼器,幫助我們處理json數據
>>> import requests >>> r = requests.get("https://github.com/timeline.json") >>> r.json() {‘message‘: ‘Hello there, wayfaring stranger. If you’re reading this then you probably didn’t 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‘} >>> r.status_code 410 >>> r.raise_for_status <bound method Response.raise_for_status of <Response [410]>>
如果json數據解碼失敗,就會拋出一個ValueError: No JSON object could be decoded的異常
但是成功調用r.json()也不能說明響應成功,有的服務器會在失敗的響應中包含一個json對象,如HTTP 500的錯誤細節,這種json也會被解碼返回
所以要檢查請求是否成功,可以使用r.status_code和r.raise_for_status來檢查
-
原始響應內容
requests獲取來自服務器的原始套接字響應
>>> import requests >>> r = requests.get("http://httpbin.org/get", stream=True) >>> r.raw <urllib3.response.HTTPResponse object at 0x000001B93F230518> >>> r.raw.read(300) b‘, \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "origin": "110.90.39.155", \n "url": "http://httpbin.org/get"\n}\n‘
要在初始請求中設置stream=True,然後用r.raw,可以使用r.raw.read()對內容進行讀取
Python模塊-requests(一)