Python爬蟲Requests庫怎么使用
這篇“Python爬蟲Requests庫怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python爬蟲Requests庫怎么使用”文章吧。
1、安裝 requests 庫
因為學習過程使用的是 Python 語言,需要提前安裝 Python ,我安裝的是 Python 3.8,可以通過命令 python --version 查看自己安裝的 Python 版本,建議安裝 Python 3.X 以上的版本。
安裝好 Python 以后可以 直接通過以下命令安裝 requests 庫。
pip?install?requests
Ps:可以切換到國內的pip源,例如阿里、豆瓣,速度快
為了演示功能,我這里使用nginx模擬了一個簡單網站。
下載好了以后,直接運行根目錄下的 nginx.exe 程序就可以了(備注:windows環境下)。
這時本機訪問 :http://127.0.0.1 ,會進入 nginx 的一個默認頁面。
2、獲取網頁
下面我們開始用 requests 模擬一個請求,獲取頁面源代碼。
import?requestsr?=?requests.get('http://127.0.0.1')print(r.text)
執行以后得到的結果如下:
<!DOCTYPE?html><html><head><title>Welcome?to?nginx!</title><style>????body?{????????width:?35em;????????margin:?0?auto;????????font-family:?Tahoma,?Verdana,?Arial,?sans-serif;????}</style></head><body><h2>Welcome?to?nginx!</h2><p>If?you?see?this?page,?the?nginx?web?server?is?successfully?installed?andworking.?Further?configuration?is?required.</p> <p>For?online?documentation?and?support?please?refer?to<a?>nginx.com</a>.</p> <p><em>Thank?you?for?using?nginx.</em></p></body></html>
3、關于請求
常見的請求有很多種,比如上面的示例使用的就是 GET 請求,這里詳細介紹一下這些常見的請求方法。
4、GET 請求
4.1、發起請求
我們使用相同的方法,發起一個 GET 請求:
import?requests??r?=?requests.get('http://httpbin.org/get')??print(r.text)
返回結果如下:
{"args":?{},?"headers":?{"Accept":?"*/*",?"Accept-Encoding":?"gzip,?deflate",?"Host":?"httpbin.org",?"User-Agent":?"python-requests/2.23.0",?"X-Amzn-Trace-Id":?"Root=1-5f846520-19f215aa46213a2b4241c18a"??},?"origin":?"xxxx",?"url":?"http://httpbin.org/get"}
通過返回結果,我們可以看到返回結果所包括的信息有:Headers、URL、IP等。
4.2、添加參數
平時我們訪問的 URL 會包含一些參數,比如:id是100,name是YOOAO。正常的訪問,我們會編寫如下 URL 進行訪問:
http://httpbin.org/get?id=100&name=YOOAO
顯然很不方便,而且參數多的情況下會容易出錯,這時我們可以通過 params 參數優化輸入內容。
import?requests??data?=?{??????'id':?'100',??????'name':?'YOOAO'}??r?=?requests.get('http://httpbin.org/get',?params=data)??print(r.text)
這是執行代碼返回的結果如下:
{"args":?{"id":?"100",?"name":?"YOOAO"??},?"headers":?{"Accept":?"*/*",?"Accept-Encoding":?"gzip,?deflate",?"Host":?"httpbin.org",?"User-Agent":?"python-requests/2.23.0",?"X-Amzn-Trace-Id":?"Root=1-5f84658a-1cd0437b4cf34835410d7161"??},?"origin":?"xxx.xxxx.xxx.xxx",?"url":?"http://httpbin.org/get?id=100&name=YOOAO"}
通過返回結果,我們可以看到,通過字典方式傳輸的參數被自動構造成了完整的 URL ,不需要我們自己手動完成構造。
4.3、返回結果處理
返回結果是 json?格式,因此我們可以使用調用 json 的方法來解析。如果返回內容不是 json?格式,這種調用會報錯。
import?requests?? r?=?requests.get('http://httpbin.org/get')??print(type(r.text))???print(type(r.json()))
返回結果:
<class?'str'><class?'dict'>
4.4、內容抓取
這里我們使用簡單的正則表達式,來抓取nginx示例頁面種所有< a >標簽的內容,代碼如下:
import?requestsimport?re r?=?requests.get('http://127.0.0.1')pattern?=?re.compile('<a.*?>(.*?)</a>',?re.S)a_content?=?re.findall(pattern,?r.text)print(a_content)
抓取結果:
['nginx.org',?'nginx.com']
這里一次簡單的頁面獲取和內容抓取就完成了,
4.5、數據文件下載
上面的示例,返回的都是頁面信息,如果我們想獲取網頁上的圖片、音頻和視頻文件,我們就需要學會抓取頁面的二進制數據。我們可以使用 open 方法來完成圖片等二進制文件的下載,示例代碼:
import?requests r?=?requests.get('http://tu.ossfiles.cn:9186/group3/M00/09/FB/rBpVfl8QFLOAYhhcAAC-pTdNj7g471.jpg')with?open('image.jpg',?'wb')?as?f:????f.write(r.content)print('下載完成')
open 方法中,它的第一個參數是文件名稱,第二個參數代表以二進制的形式打開,可以向文件里寫入二進制數據。
運行結束以后,會在運行文件的同級文件夾下保存下載下來的圖片。運用同樣原理,我們可以處理視頻和音頻文件。
4.6、添加headers
在上面的示例中,我們直接發起的請求,沒有添加 headers ,某些網站為因為請求不攜帶請求頭而造成訪問異常,這里我們可以手動添加 headers 內容,模擬添加 headers 中的 Uer-Agent 內容代碼:
import?requests headers?=?{'User-Agent':'Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10_8_3)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/27.0.1453.93?Safari/537.36'}r?=?requests.get('http://httpbin.org/get',?headers=headers)print(r.text)
執行結果:
{"args":?{},?"headers":?{"Accept":?"*/*",?"Accept-Encoding":?"gzip,?deflate",?"Host":?"httpbin.org",?"User-Agent":?"Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10_8_3)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/27.0.1453.93?Safari/537.36",?"X-Amzn-Trace-Id":?"Root=1-5ec8f342-8a9f986011eac8f07be8b450"??},?"origin":?"xxx3.xx.xxx.xxx",?"url":?"http://httpbin.org/get"}
結果可見,User-Agent 的值變了。不是之前的:python-requests/2.23.0。
5、POST 請求
GET請求相關的知識都講完了,下面講講另一個常見的請求方式:POST請求。
使用 requests 實現 POST 請求的代碼如下:
import?requestsdata?=?{??????'id':?'100',??????'name':?'YOOAO'}?? r?=?requests.post("http://httpbin.org/post",?data=data)print(r.text)
結果如下
{"args":?{},?"data":?"",?"files":?{},?"form":?{"id":?"100",?"name":?"YOOAO"??},?"headers":?{"Accept":?"*/*",?"Accept-Encoding":?"gzip,?deflate",?"Content-Length":?"17",?"Content-Type":?"application/x-www-form-urlencoded",?"Host":?"httpbin.org",?"User-Agent":?"python-requests/2.23.0",?"X-Amzn-Trace-Id":?"Root=1-5ec8f4a0-affca27a05e320a84ca6535a"??},?"json":?null,?"origin":?"xxxx",?"url":?"http://httpbin.org/post"}
從 form 中我們看到了自己提交的數據,可見我們的 POST 請求訪問成功。
6、響應
訪問URL時,有請求就會有響應,上面的示例使用 text 和 content 獲取了響應的內容。除此以外,還有很多屬性和方法可以用來獲取其他信息,比如狀態碼、響應頭、Cookies 等。
import?requests r?=?requests.get('http://127.0.0.1/')print(type(r.status_code),?r.status_code)print(type(r.headers),?r.headers)print(type(r.cookies),?r.cookies)print(type(r.url),?r.url)print(type(r.history),?r.history)
關于狀態碼,requests 還提供了一個內置的狀態碼查詢對象 requests.codes,用法示例如下:
import?requestsr?=?requests.get('http://127.0.0.1/')exit()?if?not?r.status_code?==?requests.codes.ok?else?print('Request?Successfully')==========執行結果==========Request?Successfully
這里通過比較返回碼和內置的成功的返回碼,來保證請求得到了正常響應,輸出成功請求的消息,否則程序終止。
這里我們用 requests.codes.ok 得到的是成功的狀態碼 200。
這樣的話,我們就不用再在程序里面寫狀態碼對應的數字了,用字符串表示狀態碼會顯得更加直觀。
下面是響應碼和查詢條件對照信息:
#?信息性狀態碼??100:?('continue',),??101:?('switching_protocols',),??102:?('processing',),??103:?('checkpoint',),??122:?('uri_too_long',?'request_uri_too_long'),?? #?成功狀態碼??200:?('ok',?'okay',?'all_ok',?'all_okay',?'all_good',?'\\o/',?'?'),??201:?('created',),??202:?('accepted',),??203:?('non_authoritative_info',?'non_authoritative_information'),??204:?('no_content',),??205:?('reset_content',?'reset'),??206:?('partial_content',?'partial'),??207:?('multi_status',?'multiple_status',?'multi_stati',?'multiple_stati'),??208:?('already_reported',),??226:?('im_used',),?? #?重定向狀態碼??300:?('multiple_choices',),??301:?('moved_permanently',?'moved',?'\\o-'),??302:?('found',),??303:?('see_other',?'other'),??304:?('not_modified',),??305:?('use_proxy',),??306:?('switch_proxy',),??307:?('temporary_redirect',?'temporary_moved',?'temporary'),??308:?('permanent_redirect',????????'resume_incomplete',?'resume',),?#?These?2?to?be?removed?in?3.0?? #?客戶端錯誤狀態碼??400:?('bad_request',?'bad'),??401:?('unauthorized',),??402:?('payment_required',?'payment'),??403:?('forbidden',),??404:?('not_found',?'-o-'),??405:?('method_not_allowed',?'not_allowed'),??406:?('not_acceptable',),??407:?('proxy_authentication_required',?'proxy_auth',?'proxy_authentication'),??408:?('request_timeout',?'timeout'),??409:?('conflict',),??410:?('gone',),??411:?('length_required',),??412:?('precondition_failed',?'precondition'),??413:?('request_entity_too_large',),??414:?('request_uri_too_large',),??415:?('unsupported_media_type',?'unsupported_media',?'media_type'),??416:?('requested_range_not_satisfiable',?'requested_range',?'range_not_satisfiable'),??417:?('expectation_failed',),??418:?('im_a_teapot',?'teapot',?'i_am_a_teapot'),??421:?('misdirected_request',),??422:?('unprocessable_entity',?'unprocessable'),??423:?('locked',),??424:?('failed_dependency',?'dependency'),??425:?('unordered_collection',?'unordered'),??426:?('upgrade_required',?'upgrade'),??428:?('precondition_required',?'precondition'),??429:?('too_many_requests',?'too_many'),??431:?('header_fields_too_large',?'fields_too_large'),??444:?('no_response',?'none'),??449:?('retry_with',?'retry'),??450:?('blocked_by_windows_parental_controls',?'parental_controls'),??451:?('unavailable_for_legal_reasons',?'legal_reasons'),??499:?('client_closed_request',),?? #?服務端錯誤狀態碼??500:?('internal_server_error',?'server_error',?'/o\\',?'?'),??501:?('not_implemented',),??502:?('bad_gateway',),??503:?('service_unavailable',?'unavailable'),??504:?('gateway_timeout',),??505:?('http_version_not_supported',?'http_version'),??506:?('variant_also_negotiates',),??507:?('insufficient_storage',),??509:?('bandwidth_limit_exceeded',?'bandwidth'),??510:?('not_extended',),??511:?('network_authentication_required',?'network_auth',?'network_authentication')
7、SSL 證書驗證
現在很多網站都會驗證證書,我們可以設置參數來忽略證書的驗證。
import?requests response?=?requests.get('https://XXXXXXXX',?verify=False)print(response.status_code)
或者制定本地證書作為客戶端證書:
import?requests response?=?requests.get('https://xxxxxx',?cert=('/path/server.crt',?'/path/server.key'))print(response.status_code)
注意:本地私有證書的 key 必須是解密狀態,加密狀態的 key 是不支持的。
8、設置超時
很多時候我們需要設置超時時間來控制訪問的效率,遇到訪問慢的鏈接直接跳過。
示例代碼:
import?requests#?設置超時時間為?10?秒r?=?requests.get('https://httpbin.org/get',?timeout=10)print(r.status_code)
將連接時間和讀取時間分開計算:
r?=?requests.get('https://httpbin.org/get',?timeout=(3,?10))
不添加參數,默認不設置超時時間,等同于:
r?=?requests.get('https://httpbin.org/get',?timeout=None)
9、身份認證
遇到一些網站需要輸入用戶名和密碼,我們可以通過 auth 參數進行設置。
import?requests??from?requests.auth?import?HTTPBasicAuth??#?用戶名為?admin?,密碼為?admin?r?=?requests.get('https://xxxxxx/',?auth=HTTPBasicAuth('admin',?'admin'))??print(r.status_code)
簡化寫法:
import?requests r?=?requests.get('https://xxxxxx',?auth=('admin',?'admin'))print(r.status_code)
10、設置代理
如果頻繁的訪問某個網站時,后期會被一些反爬程序識別,要求輸入驗證信息,或者其他信息,甚至IP被封無法再次訪問,這時候,我們可以通過設置代理來避免這樣的問題。
import?requests proxies?=?{??"http":?"http://10.10.1.10:3128",??"https":?"http://10.10.1.10:1080",} requests.get("http://example.org",?proxies=proxies)
若你的代理需要使用HTTP Basic Auth,可以使用?
http://user:password@host/ 語法:
proxies?=?{????"http":?"http://user:pass@10.10.1.10:3128/",}
要為某個特定的連接方式或者主機設置代理,使用 scheme://hostname 作為 key, 它會針對指定的主機和連接方式進行匹配。
proxies?=?{'http://10.20.1.128':?'http://10.10.1.10:5323'}
以上就是關于“Python爬蟲Requests庫怎么使用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注蝸牛博客行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:niceseo99@gmail.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。
評論