爬蟲筆記四
一、Requests快速入門
1.發送請求
import requests # 常見的請求方式 get post r = requests.get(‘https://github.com/timeline.json‘) # r = requests.post("http://httpbin.org/post") print(r.text) print(r.url) # 其它請求方式 # r = requests.put("http://httpbin.org/put") # r = requests.delete("http://httpbin.org/delete") # r = requests.head("http://httpbin.org/get")示例# r = requests.options("http://httpbin.org/get")
2.傳遞URL參數
手工構建的 URL,那麽數據會以鍵/值對的形式置於 URL 中,跟在一個問號的後面。例如, httpbin.org/get?key=val
。
Requests 允許你使用 params
關鍵字參數,以一個字符串字典來提供這些參數。
如果你想傳遞 key1=value1
和 key2=value2
到 httpbin.org/get
,那麽你可以使用如下代碼:
import requests payload = {‘key1‘:‘values1‘,‘示例key2‘:‘values2‘} r = requests.get(url=‘http://httpbin.org/get‘,params=payload) # params= 用來在URL中傳遞鍵值對參數 ?後面進行拼接 ?key1=values1&key2=values2 print(r.url) # http://httpbin.org/get?key1=values1&key2=values2 # 註意字典裏值為 None 的鍵都不會被添加到 URL 的查詢字符串 # 參數中的值可以是一個列表 payload = {‘key1‘:‘values1‘,‘key2‘:[‘values2‘,‘values3‘]} r = requests.get(url=‘http://httpbin.org/get‘,params=payload) print(r.url) # http://httpbin.org/get?key1=values1&key2=values2&key2=values3
3.響應內容
Requests 會自動解碼來自服務器的內容。大多數 unicode 字符集都能被無縫地解碼。
import requests r = requests.get(url=‘https://github.com/timeline.json‘) # 默認是utf8編碼 r.encoding = ‘gbk‘ # 改變編碼示例
4.Json響應內容
Requests 中也有一個內置的 JSON 解碼器,助你處理 JSON 數據。
需要註意的是,成功調用 r.json()
並**不**意味著響應的成功。有的服務器會在失敗的響應中包含一個 JSON 對象(比如 HTTP 500 的錯誤細節)。這種 JSON 會被解碼返回。要檢查請求是否成功,請使用 r.raise_for_status()
或者檢查 r.status_code
是否和你的期望相同。
r.json( )
5.定制請求頭
如果你想為請求添加 HTTP 頭部,只要簡單地傳遞一個 dict
給 headers
參數就可以了。
import requests url = ‘https://api.github.com/some/endpoint‘ headers = {‘user-agent‘: ‘my-app/0.0.1‘} r = requests.get(url, headers=headers)示例
所有的 header 值必須是 string
、bytestring 或者 unicode。
盡管傳遞 unicode header 也是允許的,但不建議這樣做。
6.更加復雜的POST請求
想要發送一些編碼為表單形式的數據——非常像一個 HTML 表單。只需簡單地傳遞一個字典給 data 參數。你的數據字典在發出請求時會自動編碼為表單形式
import requests payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} r = requests.post("http://httpbin.org/post", data=payload) print(r.text) ‘‘‘ { "args": {}, "data": "", "files": {}, "form": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.4" }, "json": null, "origin": "61.184.75.210", "url": "http://httpbin.org/post" } ‘‘‘示例
為 data
參數傳入一個元組列表。在表單中多個元素使用同一 key 的時候,這種方式尤其有效:
import requests payload = ((‘key1‘, ‘value1‘), (‘key1‘, ‘value2‘)) r = requests.post("http://httpbin.org/post", data=payload) print(r.text) ‘‘‘ { "args": {}, "data": "", "files": {}, "form": { "key1": [ "value1", "value2" ] }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.4" }, "json": null, "origin": "61.184.75.210", "url": "http://httpbin.org/post" } ‘‘‘示例
很多時候你想要發送的數據並非編碼為表單形式的。如果你傳遞一個 string
而不是一個 dict
,那麽數據會被直接發布出去。
import requests import json url = ‘https://api.github.com/some/endpoint‘ payload = {‘some‘: ‘data‘} r = requests.post(url, data=json.dumps(payload)) # r = requests.post(url, json=payload)示例
7.響應狀態碼
import requests r = requests.get(‘http://httpbin.org/get‘) print(r.status_code) # 200示例代碼
8.響應頭
我們可以查看以一個 Python 字典形式展示的服務器響應頭:
import requests r = requests.get(‘http://httpbin.org/get‘) print(r.headers) ‘‘‘ { ‘Connection‘: ‘keep-alive‘, ‘Server‘: ‘meinheld/0.6.1‘, ‘Date‘: ‘Tue, 09 Jan 2018 07:00:48 GMT‘, ‘Content-Type‘: ‘application/json‘, ‘Access-Control-Allow-Origin‘: ‘*‘, ‘Access-Control-Allow-Credentials‘: ‘true‘, ‘X-Powered-By‘: ‘Flask‘, ‘X-Processed-Time‘: ‘0.000684976577759‘, ‘Content-Length‘: ‘266‘, ‘Via‘: ‘1.1 vegur‘ } ‘‘‘示例
它還有一個特殊點,那就是服務器可以多次接受同一 header,每次都使用不同的值。
9.Cookie
要想發送你的cookies到服務器,可以使用 cookies
參數:
import requests url = ‘http://httpbin.org/cookies‘ cookies = dict(cookies_are=‘working‘) r = requests.get(url, cookies=cookies) print(r.text) ‘‘‘ { "cookies": { "cookies_are": "working" } } ‘‘‘示例
Cookie 的返回對象為 RequestsCookieJar
,它的行為和字典類似,但界面更為完整,適合跨域名跨路徑使用。你還可以把 Cookie Jar 傳到 Requests 中:
import requests jar = requests.cookies.RequestsCookieJar() jar.set(‘tasty_cookie‘, ‘yum‘, domain=‘httpbin.org‘, path=‘/cookies‘) jar.set(‘gross_cookie‘, ‘blech‘, domain=‘httpbin.org‘, path=‘/elsewhere‘) url = ‘http://httpbin.org/cookies‘ r = requests.get(url, cookies=jar) print(r.text) ‘‘‘ { "cookies": { "tasty_cookie": "yum" } } ‘‘‘示例
10重定向和請求歷史
默認情況下,除了 HEAD, Requests 會自動處理所有重定向。
可以使用響應對象的 history
方法來追蹤重定向。
Response.history
是一個 Response
對象的列表,為了完成請求而創建了這些對象。這個對象列表按照從最老到最近的請求進行排序。
import requests r = requests.get(‘http://github.com‘) print(r.url) # https://github.com/ print(r.status_code) # 200 print(r.history) # [<Response [301]>]示例
如果你使用的是GET、OPTIONS、POST、PUT、PATCH 或者 DELETE,那麽你可以通過 allow_redirects
參數禁用重定向處理:
import requests # allow_redirects=False 禁用重定向 r = requests.get(‘http://github.com‘, allow_redirects=False) print(r.url) # http://github.com/ print(r.status_code) # 301 print(r.history) # []示例
如果你使用了 HEAD,你也可以啟用重定向:
import requests r = requests.head(‘http://github.com‘, allow_redirects=True) print(r.url) # https://github.com/ print(r.status_code) # 200 print(r.history) # [<Response [301]>]示例
11.超時
你可以告訴 requests 在經過以 timeout
參數設定的秒數時間之後停止等待響應。
基本上所有的生產代碼都應該使用這一參數。如果不使用,你的程序可能會永遠失去響應:
>>> requests.get(‘http://github.com‘, timeout=0.001) Traceback (most recent call last): File "<stdin>", line 1, in <module> requests.exceptions.Timeout: HTTPConnectionPool(host=‘github.com‘, port=80): Request timed out. (timeout=0.001) 註意 timeout 僅對連接過程有效,與響應體的下載無關。 timeout 並不是整個下載響應的時間限制,而是如果服務器在 timeout 秒內沒有應答,將會引發一個異常(更精確地說,是在 timeout 秒內沒有從基礎套接字上接收到任何字節的數據時)If no timeout is specified explicitly, requests do not time out.示例
12.錯誤與異常
遇到網絡問題(如:DNS 查詢失敗、拒絕連接等)時,Requests 會拋出一個 ConnectionError
異常。
如果 HTTP 請求返回了不成功的狀態碼, Response.raise_for_status()
會拋出一個 HTTPError
異常。
若請求超時,則拋出一個 Timeout
異常。
若請求超過了設定的最大重定向次數,則會拋出一個 TooManyRedirects
異常。
所有Requests顯式拋出的異常都繼承自 requests.exceptions.RequestException
。
爬蟲筆記四