python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
requests庫
利用pip安裝:
pip install requests
基本請(qǐng)求
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請(qǐng)求
參數(shù)是字典,我們可以傳遞json類型的參數(shù):
import requests
from fake_useragent import UserAgent#請(qǐng)求頭部庫
headers = {"User-Agent":UserAgent().random}#獲取一個(gè)隨機(jī)的請(qǐng)求頭
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請(qǐng)求
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 請(qǐng)求
參數(shù)也是字典,也可以傳遞json類型的參數(shù):
import requests
from fake_useragent import UserAgent
headers = {"User-Agent":UserAgent().random}
url = "https://www.baidu.cn/index/login/login" #登錄賬號(hào)密碼的網(wǎng)址
params = {
"user":"1351351335",#賬號(hào)
"password":"123456"#密碼
}
response = requests.post(url,headers=headers,data=params)
response.text

因?yàn)檫@里需要一個(gè)登錄的網(wǎng)頁,我這里就隨便用了一個(gè),沒有登錄,所以顯示的結(jié)果是這樣的,如果想要測(cè)試登錄的效果,請(qǐng)找一個(gè)登錄的頁面去嘗試一下。
3.IP代理
采集時(shí)為避免被封IP,經(jīng)常會(huì)使用代理,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:端口號(hào)
#"http":"https://182.145.31.211:4224"# 或者IP:端口號(hào)
}
requests.get(url,headers=headers,proxies=proxies)
代理IP可以去:快代理去找,也可以去購買。
http://httpbin.org/get。這個(gè)網(wǎng)址是查看你現(xiàn)在的信息:

4.設(shè)置訪問超時(shí)時(shí)間
可以通過timeout屬性設(shè)置超時(shí)時(shí)間,一旦超過這個(gè)時(shí)間還沒獲取到響應(yīng)內(nèi)容,就會(huì)提示錯(cuò)誤。
#設(shè)置訪問時(shí)間
requests.get("http://baidu.com/",timeout=0.1)

5.證書問題(SSLError:HTTP)
ssl驗(yàn)證。
import requests
from fake_useragent import UserAgent #請(qǐng)求頭部庫
url = "https://www.12306.cn/index/" #需要證書的網(wǎng)頁地址
headers = {"User-Agent":UserAgent().random}#獲取一個(gè)隨機(jī)請(qǐng)求頭
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自動(dòng)保存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" #登錄完賬號(hào)密碼以后的網(wǎng)頁地址
resp = session.get(info_url,headers=headers)
resp.text
因?yàn)槲疫@里沒有使用需要賬號(hào)密碼的網(wǎng)頁,所以顯示這樣:

我獲取了一個(gè)智慧樹的網(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" #登錄完賬號(hào)密碼以后的網(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 | 請(qǐng)求頭內(nèi)容 |
| resp.cookie | 獲取cookie |
到此這篇關(guān)于python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)的文章就介紹到這了,更多相關(guān)python爬蟲requests庫用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
將Python項(xiàng)目打包成exe并附帶下載功能的操作流程
這篇文章主要為大家詳細(xì)介紹了將Python項(xiàng)目打包成exe并附帶下載功能的操作流程,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下2023-12-12
PyTorch?使用torchvision進(jìn)行圖片數(shù)據(jù)增廣
本文主要介紹了PyTorch?使用torchvision進(jìn)行圖片數(shù)據(jù)增廣,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Python庫?Bokeh?數(shù)據(jù)可視化實(shí)用指南
大家好,今天跟大家分享的是交互式可視化神器?Python?Bokeh?的詳細(xì)使用教程,Bokeh是一個(gè)面向現(xiàn)代web瀏覽器的交互式可視化庫。它提供了多功能圖形的優(yōu)雅、簡潔的構(gòu)造,并在大型數(shù)據(jù)集或流式數(shù)據(jù)集上提供了高性能的交互性,接下來讓我們?cè)敿?xì)看看吧2021-11-11
Python中Cryptography庫實(shí)現(xiàn)加密解密
Python中Cryptography庫給你的文件加把安全鎖,本文主要介紹了Python中Cryptography庫實(shí)現(xiàn)加密解密,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02

