Python爬取求職網(wǎng)requests庫(kù)和BeautifulSoup庫(kù)使用詳解
一、requests庫(kù)
1、requests簡(jiǎn)介
requests庫(kù)就是一個(gè)發(fā)起請(qǐng)求的第三方庫(kù),requests允許你發(fā)送HTTP/1.1 請(qǐng)求,你不需要手動(dòng)為 URL 添加查詢(xún)字串,也不需要對(duì) POST 數(shù)據(jù)進(jìn)行表單編碼。Keep-alive 和 HTTP 連接池的功能是 100% 自動(dòng)化的,一切動(dòng)力都來(lái)自于根植在 requests 內(nèi)部的 urllib3。簡(jiǎn)單來(lái)說(shuō)有了這個(gè)庫(kù),我們就能輕而易舉向?qū)?yīng)的網(wǎng)站發(fā)起請(qǐng)求,從而對(duì)網(wǎng)頁(yè)數(shù)據(jù)進(jìn)行獲取,還可以獲取服務(wù)器返回的響應(yīng)內(nèi)容和狀態(tài)碼。
requesets中文文檔頁(yè)面https://requests.kennethreitz.org/zh_CN/latest/
2、安裝requests庫(kù)
一般電腦安裝的Python都會(huì)自帶這個(gè)庫(kù),如果沒(méi)有就可在命令行輸入下面這行代碼安裝
pip install requests
3、使用requests獲取網(wǎng)頁(yè)數(shù)據(jù) 我們先導(dǎo)入模塊
import requests
- 對(duì)想要獲取數(shù)據(jù)的網(wǎng)站發(fā)起請(qǐng)求,以下以qq音樂(lè)官網(wǎng)為例
res = requests.get('https://y.qq.com/') #發(fā)起請(qǐng)求 print(res) #輸出<Response [200]>
輸出的200其實(shí)就是一個(gè)響應(yīng)狀態(tài)碼,下面給大家列出有可能返回的各狀態(tài)碼含義
狀態(tài)碼 | 含義 |
---|---|
1xx | 繼續(xù)發(fā)送信息 |
2xx | 請(qǐng)求成功 |
3xx | 重定向 |
4xx | 客戶(hù)端錯(cuò)誤 |
5xx | 服務(wù)端錯(cuò)誤 |
- 獲取qq音樂(lè)首頁(yè)的網(wǎng)頁(yè)源代碼
res = requests.get('https://y.qq.com/') #發(fā)起請(qǐng)求 print(res.text) #res.text就是網(wǎng)頁(yè)的源代碼
4、總結(jié)requests的一些方法
屬性 | 含義 |
---|---|
res.status_code | HTTP的狀態(tài)碼 |
res.text | 響應(yīng)內(nèi)容的文本 |
res.content | 響應(yīng)內(nèi)容的二進(jìn)制形式文本 |
res.encoding | 響應(yīng)內(nèi)容的編碼 |
既然我們學(xué)好了如何獲取網(wǎng)頁(yè)源代碼,接下來(lái)我們就學(xué)習(xí)下怎么用BeautifulSoup庫(kù)對(duì)我們獲取的內(nèi)容進(jìn)行提取。
二、BeautifulSoup庫(kù)
1、BeautifulSoup簡(jiǎn)介
BeautifulSoup是Python里的第三方庫(kù),處理數(shù)據(jù)十分實(shí)用,有了這個(gè)庫(kù),我們就可以根據(jù)網(wǎng)頁(yè)源代碼里對(duì)應(yīng)的HTML標(biāo)簽對(duì)數(shù)據(jù)進(jìn)行有目的性的提取。BeautifulSoup庫(kù)一般與requests庫(kù)搭配使用。 不熟的HTML標(biāo)簽的最好去百度下,了解一些常用的標(biāo)簽。
2、安裝BeautifulSoup庫(kù)
同樣的如果沒(méi)用這個(gè)庫(kù),可以通過(guò)命令行輸入下列代碼安裝
pip install beautifulsoup4
3、使用BeautifulSoup解析并提取獲取的數(shù)據(jù)
import requests from bs4 import BeautifulSoup header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} res = requests.get('https://y.qq.com/',headers=header) #headers是一種反爬蟲(chóng)措施 soup = BeautifulSoup(res.text,'html.parser') #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器 print(soup) #輸出qq音樂(lè)首頁(yè)的源代碼
部分輸出結(jié)果
看到輸出結(jié)果,我們已經(jīng)成功將網(wǎng)頁(yè)源代碼解析成BeautifulSoup對(duì)象。這時(shí)可能有人就會(huì)問(wèn)res.text輸出的不就是網(wǎng)頁(yè)代碼了嗎,何苦再將它轉(zhuǎn)為BeautifulSoup對(duì)象呢?
我們先來(lái)通過(guò)type()函數(shù)看下它們的類(lèi)型
import requests from bs4 import BeautifulSoup header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} res = requests.get('https://y.qq.com/',headers=header) #headers是一種反爬蟲(chóng)措施 soup = BeautifulSoup(res.text,'html.parser') #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器 print(type(res.text)) print(type(soup))
輸出結(jié)果
我們可以看到res.text的類(lèi)型是字符串類(lèi)型,而soup則是BeautifulSoup對(duì)象類(lèi)型。相比于res.text的字符串類(lèi)型,soup的BeautifulSoup對(duì)象類(lèi)型擁有著更多可用的方法,以便我們快速提取出需要的數(shù)據(jù)。這就是為什么我們要多此一步了。
4、BeautifulSoup提取數(shù)據(jù)的方法
- 先來(lái)了解兩個(gè)最常用的方法
方法 | 作用 |
---|---|
find() | 返回第一個(gè)符合要求的數(shù)據(jù) |
find_all() | 返回所有符合要求的數(shù)據(jù) |
這兩個(gè)函數(shù)傳入的參數(shù)就是我們對(duì)數(shù)據(jù)的篩選條件了,我們可以向這兩個(gè)函數(shù)分別傳入什么參數(shù)呢?
我們以下面在qq音樂(lè)首頁(yè)截取的源代碼片段為例,試用兩個(gè)函數(shù)
<div class="index__hd"> <h2 class="index__tit"><i class="icon_txt">歌單推薦</i></h2> </div> <!-- 切換 --> <div class="mod_index_tab" data-stat="y_new.index.playlist"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item index_tab__item--current js_tag" data-index="0" data-type="recomPlaylist" data-id="1">為你推薦</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item js_tag" data-type="playlist" data-id="3056">網(wǎng)絡(luò)歌曲</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item js_tag" data-type="playlist" data-id="3256">綜藝</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item js_tag" data-type="playlist" data-id="59">經(jīng)典</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item js_tag" data-type="playlist" data-id="3317">官方歌單</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="index_tab__item js_tag" data-type="playlist" data-id="71">情歌</a> </div>
find()函數(shù)
如果想要獲取歌單推薦這一行的內(nèi)容,我們就需要先對(duì)歌單推薦的HTML標(biāo)簽進(jìn)行識(shí)別,我們發(fā)現(xiàn)它在class="icon_txt"的i標(biāo)簽下,接著就可以通過(guò)以下這種方法進(jìn)行提取
import requests from bs4 import BeautifulSoup header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} res = requests.get('https://y.qq.com/',headers=header) #headers是一種反爬蟲(chóng)措施 soup = BeautifulSoup(res.text,'html.parser') #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器 print(soup.find('i', class_='icon_txt')) #找到 class_='icon_txt'的 i 標(biāo)簽
因?yàn)?class 是 Python 中定義類(lèi)的關(guān)鍵字,所以用 class_ 表示 HTML 中的 class
輸出結(jié)果
find_all()函數(shù)
如果我們想要把歌單推薦的全部主題提取下來(lái)的話(huà),就要用到find_all()函數(shù)
同樣的,我們發(fā)現(xiàn)這幾個(gè)主題都在 class="index_tab__item js_tag"的 a標(biāo)簽下,這時(shí)為了避免篩選到源代碼中其他同為class="index_tab__item js_tag"的標(biāo)簽,我們需要再加多一個(gè)條件data-type=“playlist”,具體怎么操作呢?
import requests from bs4 import BeautifulSoup header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} res = requests.get('https://y.qq.com/',headers=header) #headers是一種反爬蟲(chóng)措施 soup = BeautifulSoup(res.text,'html.parser') #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器 print(soup.find('i', class_='icon_txt')) items = soup.find_all('a',attrs={"class" :"index_tab__item js_tag","data-type":"playlist"})
實(shí)現(xiàn)的方法就是在第二個(gè)參數(shù)處傳入一個(gè)鍵值對(duì),在里面添加篩選的屬性
輸出結(jié)果
通過(guò)上面兩個(gè)小案例,我們發(fā)現(xiàn)find()和find_all()函數(shù)返回的是Tag對(duì)象和Tag對(duì)象組成的列表,而我們需要的并不是這一大串東西,我們需要的只是Tag對(duì)象的text屬性或者h(yuǎn)ref(鏈接)屬性,實(shí)現(xiàn)代碼如下
import requests from bs4 import BeautifulSoup header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} res = requests.get('https://y.qq.com/',headers=header) #headers是一種反爬蟲(chóng)措施 soup = BeautifulSoup(res.text,'html.parser') #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器 tag1=soup.find('i', class_='icon_txt') print(tag1.text) items = soup.find_all('a',attrs={"class" :"index_tab__item js_tag","data-type":"playlist"}) for i in items: #遍歷列表 tag2=i.text print(tag2)
輸出結(jié)果
這樣我們就成功把主題的文本內(nèi)容獲取了,而想要提取標(biāo)簽中的屬性值,則可以用對(duì)象名[‘屬性']的方法獲取,這里就不演示了
本次分享就到這里了,在下次介紹完反爬蟲(chóng)和如何將數(shù)據(jù)寫(xiě)進(jìn)文件的方法后,我會(huì)結(jié)合我所寫(xiě)的三篇文章的方法來(lái)做一個(gè)爬取求職網(wǎng)的實(shí)例跟大家分享,有興趣的可以看下,謝謝大家!
希望大家以后多多支持腳本之家!
相關(guān)文章
python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式
這篇文章主要介紹了python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05使用apiDoc實(shí)現(xiàn)python接口文檔編寫(xiě)
今天小編就為大家分享一篇使用apiDoc實(shí)現(xiàn)python接口文檔編寫(xiě),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python matplotlib以日期為x軸作圖代碼實(shí)例
這篇文章主要介紹了Python matplotlib以日期為x軸作圖代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11基于Python實(shí)現(xiàn)一個(gè)自動(dòng)關(guān)機(jī)程序并打包成exe文件
這篇文章主要介紹了通過(guò)Python創(chuàng)建一個(gè)可以自動(dòng)關(guān)機(jī)的小程序,并打包成exe文件。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的同學(xué)可以了解一下2021-12-12Python Loguru輕松靈活的日志管理庫(kù)基本用法探索
Loguru是一個(gè)用于Python的高性能、簡(jiǎn)潔且靈活的日志庫(kù),它的目標(biāo)是提供一種簡(jiǎn)單的方式來(lái)記錄應(yīng)用程序的運(yùn)行情況,同時(shí)保持代碼的簡(jiǎn)潔性和可讀性,本文將探索loguru的基本用法2024-01-01Python內(nèi)置模塊Collections的使用教程詳解
collections 是 Python 的一個(gè)內(nèi)置模塊,所謂內(nèi)置模塊的意思是指 Python 內(nèi)部封裝好的模塊,無(wú)需安裝即可直接使用。本文將詳解介紹Collections的使用方式,需要的可以參考一下2022-03-03