Python爬蟲 Requests庫的使用

2021-10-09 05:21:31 字數 1249 閱讀 4127

方法

說明requests.request()

構造乙個請求,支撐以下各方法的基礎方法

requests.get()

獲取html網頁的主要方法,對應於http的get

requests.head()

獲取html網頁頭資訊的主要方法,對應於http的head

requests.post()

向html網頁提交post請求的方法,對應於http的post

requests.put()

向html網頁提交put請求的方法,對應於http的put

requests.patch()

向html網頁提交區域性修改請求,對應於http的patch

requests.delete()

向html網頁提交刪除請求,對應於http的delete

requests.get(url , params=

none

,**kwargs)

url: 擬獲取頁面的url鏈結

params: url中的額外引數,字典或位元組流格式,可選

**kwargs: 12個控制訪問的引數

屬性說明

r.status_code

http請求的返回狀態,200表示連線成功,404表示失敗

r.text

http響應內容的字串形式,即,url對應的頁面內容

r.encoding

從http header中猜測的響應內容編碼方式

從內容中分析出的響應內容編碼方式(備選編碼方式)

r.content

http響應內容的二進位制形式

異常說明

request.connectionerror

網路連線錯誤異常,如dns查詢失敗、拒絕連線等

http錯誤異常

requests.urlrequired

url缺失異常

requests.toomanyredirects

超過最大重定向次數,產生重定向異常

requests.connecttimeout

連線遠端伺服器超時異常

requests.timeout

請求url超時,產生超時異常

r.raise_for_status()

如果不是200,產生異常requests.httperror

Python爬蟲 Request模組

文章說明了request模組的意義,且強調了request模組使用更加方便。接下來介紹幾種常用的request操作,並且會在後續補充說明一些特定用法。匯入檔案 import requests一 請求 右邊為請求語句,返回值為response回應 r requests.get r requests.p...

python爬蟲利器 request庫

request庫比urllib2庫更為高階,因為其功能更強大,更易於使用。使用該庫可以十分方便我們的抓取。基本請求 r requests.get r requests.post r requests.put r requests.delete r requests.head r requests.o...

爬蟲 python(二)初識request

from urllib.request import urlopen 傳送請求,獲取伺服器給的響應 url response urlopen url 讀取結果,無法正常顯示中文 html response.read 進行解碼操作,轉為utf 8 html decode html.decode 列印結...