python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
requests庫
利用pip安裝:
pip install requests
基本請求
req = requests.get("https://www.baidu.com/") req = requests.post("https://www.baidu.com/") req = requests.put("https://www.baidu.com/") req = requests.delete("https://www.baidu.com/") req = requests.head("https://www.baidu.com/") req = requests.options(https://www.baidu.com/)
1.get請求
參數(shù)是字典,我們可以傳遞json類型的參數(shù):
import requests from fake_useragent import UserAgent#請求頭部庫 headers = {"User-Agent":UserAgent().random}#獲取一個隨機(jī)的請求頭 url = "https://www.baidu.com/s"#網(wǎng)址 params={ "wd":"豆瓣" #網(wǎng)址的后綴 } requests.get(url,headers=headers,params=params)
返回了狀態(tài)碼,所以我們要想獲取內(nèi)容,需要將其轉(zhuǎn)成text:
#get請求 headers = {"User-Agent":UserAgent().random} url = "https://www.baidu.com/s" params={ "wd":"豆瓣" } response = requests.get(url,headers=headers,params=params) response.text
2.post 請求
參數(shù)也是字典,也可以傳遞json類型的參數(shù):
import requests from fake_useragent import UserAgent headers = {"User-Agent":UserAgent().random} url = "https://www.baidu.cn/index/login/login" #登錄賬號密碼的網(wǎng)址 params = { "user":"1351351335",#賬號 "password":"123456"#密碼 } response = requests.post(url,headers=headers,data=params) response.text
因?yàn)檫@里需要一個登錄的網(wǎng)頁,我這里就隨便用了一個,沒有登錄,所以顯示的結(jié)果是這樣的,如果想要測試登錄的效果,請找一個登錄的頁面去嘗試一下。
3.IP代理
采集時為避免被封IP,經(jīng)常會使用代理,requests也有相應(yīng) 的proxies屬性。
#IP代理 import requests from fake_useragent import UserAgent headers = {"User-Agent":UserAgent().random} url = "http://httpbin.org/get" #返回當(dāng)前IP的網(wǎng)址 proxies = { "http":"http://yonghuming:123456@192.168.1.1:8088"#http://用戶名:密碼@IP:端口號 #"http":"https://182.145.31.211:4224"# 或者IP:端口號 } requests.get(url,headers=headers,proxies=proxies)
代理IP可以去:快代理去找,也可以去購買。
http://httpbin.org/get。這個網(wǎng)址是查看你現(xiàn)在的信息:
4.設(shè)置訪問超時時間
可以通過timeout屬性設(shè)置超時時間,一旦超過這個時間還沒獲取到響應(yīng)內(nèi)容,就會提示錯誤。
#設(shè)置訪問時間 requests.get("http://baidu.com/",timeout=0.1)
5.證書問題(SSLError:HTTP)
ssl驗(yàn)證。
import requests from fake_useragent import UserAgent #請求頭部庫 url = "https://www.12306.cn/index/" #需要證書的網(wǎng)頁地址 headers = {"User-Agent":UserAgent().random}#獲取一個隨機(jī)請求頭 requests.packages.urllib3.disable_warnings()#禁用安全警告 response = requests.get(url,verify=False,headers=headers) response.encoding = "utf-8" #用來顯示中文,進(jìn)行轉(zhuǎn)碼 response.text
6.session自動保存cookies
import requests from fake_useragent import UserAgent headers = {"User-Agent":UserAgent().chrome} login_url = "https://www.baidu.cn/index/login/login" #需要登錄的網(wǎng)頁地址 params = { "user":"yonghuming",#用戶名 "password":"123456"#密碼 } session = requests.Session() #用來保存cookie #直接用session 歹意requests response = session.post(login_url,headers=headers,data=params) info_url = "https://www.baidu.cn/index/user.html" #登錄完賬號密碼以后的網(wǎng)頁地址 resp = session.get(info_url,headers=headers) resp.text
因?yàn)槲疫@里沒有使用需要賬號密碼的網(wǎng)頁,所以顯示這樣:
我獲取了一個智慧樹的網(wǎng)頁
#cookie import requests from fake_useragent import UserAgent headers = {"User-Agent":UserAgent().chrome} login_url = "https://passport.zhihuishu.com/login?service=https://onlineservice.zhihuishu.com/login/gologin" #需要登錄的網(wǎng)頁地址 params = { "user":"12121212",#用戶名 "password":"123456"#密碼 } session = requests.Session() #用來保存cookie #直接用session 歹意requests response = session.post(login_url,headers=headers,data=params) info_url = "https://onlne5.zhhuishu.com/onlinWeb.html#/stdetInex" #登錄完賬號密碼以后的網(wǎng)頁地址 resp = session.get(info_url,headers=headers) resp.encoding = "utf-8" resp.text
7.獲取響應(yīng)信息
代碼 | 含義 |
---|---|
resp.json() | 獲取響應(yīng)內(nèi)容 (以json字符串) |
resp.text | 獲取相應(yīng)內(nèi)容(以字符串) |
resp.content | 獲取響應(yīng)內(nèi)容(以字節(jié)的方式) |
resp.headers | 獲取響應(yīng)頭內(nèi)容 |
resp.url | 獲取訪問地址 |
resp.encoding | 獲取網(wǎng)頁編碼 |
resp.request.headers | 請求頭內(nèi)容 |
resp.cookie | 獲取cookie |
到此這篇關(guān)于python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)的文章就介紹到這了,更多相關(guān)python爬蟲requests庫用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python+Matplotlib制作時序動態(tài)圖
時序圖是一個二維圖,橫軸表示對象,縱軸表示時間,消息在各對象之間橫向傳遞,依照時間順序縱向排列,可以直觀的描述并發(fā)進(jìn)程,所以本文就使用Python和Matplotlib制作一個簡單的時許動態(tài)圖,感興趣的跟著小編一起來看看吧2023-07-07Python Vaex實(shí)現(xiàn)快速分析100G大數(shù)據(jù)量
Vaex是一個開源的DataFrame庫,它可以對表格數(shù)據(jù)集進(jìn)行可視化、探索、分析,甚至機(jī)器學(xué)習(xí),這些數(shù)據(jù)集和你的硬盤驅(qū)動器一樣大。本文就來聊聊如何利用Vaex實(shí)現(xiàn)快速分析100G大數(shù)據(jù)量,需要的可以參考一下2023-03-03使用Python實(shí)現(xiàn)將數(shù)據(jù)寫入Excel工作表
在數(shù)據(jù)處理和報告生成等工作中,Excel?表格是一種常見且廣泛使用的工具,本文中將介紹如何使用?Python?寫入數(shù)據(jù)到?Excel?表格,并提供更高效和準(zhǔn)確的?Excel?表格數(shù)據(jù)寫入方案,需要的可以參考下2024-01-01mac 上配置Pycharm連接遠(yuǎn)程服務(wù)器并實(shí)現(xiàn)使用遠(yuǎn)程服務(wù)器Python解釋器的方法
這篇文章主要介紹了mac 上如何配置Pycharm連接遠(yuǎn)程服務(wù)器并實(shí)現(xiàn)使用遠(yuǎn)程服務(wù)器Python解釋器,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實(shí)例
今天小編就為大家分享一篇Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06