国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

2022-02-15 15:30HuiSoul Python

這篇文章主要為大家介紹了Python爬取求職網(wǎng)及其他網(wǎng)頁時(shí)requests庫和BeautifulSoup庫的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助

 

一、requests庫

 

1、requests簡介

requests庫就是一個(gè)發(fā)起請求的第三方庫,requests允許你發(fā)送HTTP/1.1 請求,你不需要手動(dòng)為 URL 添加查詢字串,也不需要對 POST 數(shù)據(jù)進(jìn)行表單編碼。Keep-alive 和 HTTP 連接池的功能是 100% 自動(dòng)化的,一切動(dòng)力都來自于根植在 requests 內(nèi)部的 urllib3。簡單來說有了這個(gè)庫,我們就能輕而易舉向?qū)?yīng)的網(wǎng)站發(fā)起請求,從而對網(wǎng)頁數(shù)據(jù)進(jìn)行獲取,還可以獲取服務(wù)器返回的響應(yīng)內(nèi)容和狀態(tài)碼。

requesets中文文檔頁面https://requests.kennethreitz.org/zh_CN/latest/

 

2、安裝requests庫

一般電腦安裝的Python都會(huì)自帶這個(gè)庫,如果沒有就可在命令行輸入下面這行代碼安裝

pip install requests

 

3、使用requests獲取網(wǎng)頁數(shù)據(jù) 我們先導(dǎo)入模塊

import requests
  • 對想要獲取數(shù)據(jù)的網(wǎng)站發(fā)起請求,以下以qq音樂官網(wǎng)為例
res = requests.get("https://y.qq.com/") #發(fā)起請求
print(res) #輸出<Response [200]>

輸出的200其實(shí)就是一個(gè)響應(yīng)狀態(tài)碼,下面給大家列出有可能返回的各狀態(tài)碼含義

狀態(tài)碼 含義
1xx 繼續(xù)發(fā)送信息
2xx 請求成功
3xx 重定向
4xx 客戶端錯(cuò)誤
5xx 服務(wù)端錯(cuò)誤
  • 獲取qq音樂首頁的網(wǎng)頁源代碼
res = requests.get("https://y.qq.com/")  #發(fā)起請求
print(res.text)   #res.text就是網(wǎng)頁的源代碼

 

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)頁源代碼,接下來我們就學(xué)習(xí)下怎么用BeautifulSoup庫對我們獲取的內(nèi)容進(jìn)行提取。

 

二、BeautifulSoup庫

 

1、BeautifulSoup簡介

BeautifulSoup是Python里的第三方庫,處理數(shù)據(jù)十分實(shí)用,有了這個(gè)庫,我們就可以根據(jù)網(wǎng)頁源代碼里對應(yīng)的HTML標(biāo)簽對數(shù)據(jù)進(jìn)行有目的性的提取。BeautifulSoup庫一般與requests庫搭配使用。 不熟的HTML標(biāo)簽的最好去百度下,了解一些常用的標(biāo)簽。

 

2、安裝BeautifulSoup庫

同樣的如果沒用這個(gè)庫,可以通過命令行輸入下列代碼安裝

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是一種反爬蟲措施
soup = BeautifulSoup(res.text,"html.parser")  #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器
print(soup)  #輸出qq音樂首頁的源代碼

部分輸出結(jié)果

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

看到輸出結(jié)果,我們已經(jīng)成功將網(wǎng)頁源代碼解析成BeautifulSoup對象。這時(shí)可能有人就會(huì)問res.text輸出的不就是網(wǎng)頁代碼了嗎,何苦再將它轉(zhuǎn)為BeautifulSoup對象呢?
我們先來通過type()函數(shù)看下它們的類型

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是一種反爬蟲措施
soup = BeautifulSoup(res.text,"html.parser")  #第一個(gè)參數(shù)是HTML文本,第二個(gè)參數(shù)html.parser是Python內(nèi)置的編譯器
print(type(res.text))
print(type(soup))

輸出結(jié)果

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

我們可以看到res.text的類型是字符串類型,而soup則是BeautifulSoup對象類型。相比于res.text的字符串類型,soup的BeautifulSoup對象類型擁有著更多可用的方法,以便我們快速提取出需要的數(shù)據(jù)。這就是為什么我們要多此一步了。

 

4、BeautifulSoup提取數(shù)據(jù)的方法

  • 先來了解兩個(gè)最常用的方法
方法 作用
find() 返回第一個(gè)符合要求的數(shù)據(jù)
find_all() 返回所有符合要求的數(shù)據(jù)

這兩個(gè)函數(shù)傳入的參數(shù)就是我們對數(shù)據(jù)的篩選條件了,我們可以向這兩個(gè)函數(shù)分別傳入什么參數(shù)呢?

我們以下面在qq音樂首頁截取的源代碼片段為例,試用兩個(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)容,我們就需要先對歌單推薦的HTML標(biāo)簽進(jìn)行識別,我們發(fā)現(xiàn)它在class="icon_txt"的i標(biāo)簽下,接著就可以通過以下這種方法進(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是一種反爬蟲措施
	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 中定義類的關(guān)鍵字,所以用 class_ 表示 HTML 中的 class

輸出結(jié)果

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

find_all()函數(shù)

如果我們想要把歌單推薦的全部主題提取下來的話,就要用到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是一種反爬蟲措施
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è)鍵值對,在里面添加篩選的屬性

輸出結(jié)果

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

通過上面兩個(gè)小案例,我們發(fā)現(xiàn)find()和find_all()函數(shù)返回的是Tag對象和Tag對象組成的列表,而我們需要的并不是這一大串東西,我們需要的只是Tag對象的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是一種反爬蟲措施
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é)果

Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解

這樣我們就成功把主題的文本內(nèi)容獲取了,而想要提取標(biāo)簽中的屬性值,則可以用對象名[‘屬性"]的方法獲取,這里就不演示了

本次分享就到這里了,在下次介紹完反爬蟲和如何將數(shù)據(jù)寫進(jìn)文件的方法后,我會(huì)結(jié)合我所寫的三篇文章的方法來做一個(gè)爬取求職網(wǎng)的實(shí)例跟大家分享,有興趣的可以看下,謝謝大家!

希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/huisoul/article/details/116051442

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕一区二区三区在线视频 | 精品久久久av | 午夜激情影院 | 福利国产 | 操久在线| www.欧美 | 黄色免费网 | 国产亚洲在线 | 国产欧美日韩一区 | 国产精品伦一区二区三级视频 | 日韩精品一区二区三区中文字幕 | 国产高清在线视频 | 日本中文在线 | 永久免费在线 | 日韩激情免费视频 | 这里只有精品在线 | 91黄色免费视频 | 午夜精品影院 | 国产在线看片 | 成人精品一区二区 | 最近高清无吗免费看 | 国产精品久久久久久久久久东京 | 日韩激情一区 | 四虎影院网 | 九色国产| 国产毛片欧美毛片久久久 | 久久大陆| 日韩一区不卡 | 日韩在线欧美 | 亚洲国产精品尤物yw在线观看 | 一区二区免费在线播放 | 亚洲深深色噜噜狠狠网站 | 香蕉影院在线观看 | 日韩精品一区二区在线观看 | 91久久 | 国产片av在线永久免费观看 | 欧美一区久久 | 高清视频一区 | 欧美在线一区二区 | 在线精品国产一区二区三区 | 日韩av成人|