欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解

 更新時(shí)間:2023年06月27日 08:35:29   作者:素年涼音  
Beautiful?Soup?是一個(gè)可以從?HTML?或?XML?文件中提取數(shù)據(jù)的?Python?庫(kù),這篇文章主要來(lái)和大家介紹一下BeautifulSoup4的用法,需要的可以參考一下

BeautifulSoup 是什么

Beautiful Soup 是一個(gè)可以從 HTML 或 XML 文件中提取數(shù)據(jù)的 Python 庫(kù)。,最主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù)。能夠通過(guò)自己喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式。

注:BeautifulSoup3目前已經(jīng)停止開發(fā),官網(wǎng)推薦在現(xiàn)在的項(xiàng)目中使用BeautifulSoup4

bs4的安裝

可以在 Pycharm 中,輸入以下語(yǔ)句:然后可根據(jù)提示進(jìn)行安裝。

from bs4 import BeautifulSoup

注意:bs4 是依賴 lxml 庫(kù)的,只有先安裝 lxml 庫(kù)才可以安裝bs4庫(kù)*

文檔解析器優(yōu)缺點(diǎn)

推薦使用 lxml 作為解析器,因?yàn)樾矢摺?/p>

bs4 的使用

  • 導(dǎo)入解析包;
  • 創(chuàng)建 beautifulsoup 解析對(duì)象;
  • 打印對(duì)應(yīng)內(nèi)容即可;

代碼實(shí)例:

from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a  class="sister" id="link1">Elsie</a>,
<a  class="sister" id="link2">Lacie</a> and
<a  class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
# 創(chuàng)建一個(gè) soup 對(duì)象
soup = BeautifulSoup(html_doc, 'lxml')
print(soup, type(soup))  # <class 'bs4.BeautifulSoup'>
# 格式化文檔輸出
print(soup.prettify())
# 獲取 title 標(biāo)簽的名稱 title
print(soup.title.name)  # title
# 獲取 title 標(biāo)簽內(nèi)容
print(soup.title)  # <title>The Dormouse's story</title>
# title 標(biāo)簽里面的文本內(nèi)容
print(soup.title.string)
# 獲取 p 段落
print(soup.p)

bs4的對(duì)象種類

  • tag : html中的標(biāo)簽??梢酝ㄟ^(guò) BeautifulSoup 分析 Tag 的具體內(nèi)容,具體格式為:soup.name,其中 name 是html 下的標(biāo)簽。
  • NavigableString : 標(biāo)簽中的文本對(duì)象。
  • BeautifulSoup : 整個(gè)html文本對(duì)象,可以作為Tag對(duì)象。
  • Comment:特殊的 NavigableString 對(duì)象,如果 html標(biāo)簽中有注釋,則可過(guò)濾注釋符號(hào)并保留注釋文本。

代碼實(shí)例

from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a  class="sister" id="link1">Elsie</a>,
<a  class="sister" id="link2">Lacie</a> and
<a  class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
"""
tag:標(biāo)簽
NavigableString:可導(dǎo)航的字符串,標(biāo)簽中的文本對(duì)象
beautifulSoup:bs對(duì)象,整個(gè) html 文本對(duì)象
Comment:注釋,如果 html 標(biāo)簽中有注釋,則可過(guò)濾注釋符號(hào)并保留注釋文本
"""
# html_doc 表示要解析的文檔,而 html.parser 表示解析文檔時(shí)所用的解析器
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup)
""" tag:標(biāo)簽"""
print(type(soup.title))
print(type(soup.p))
print(type(soup.a))
""" NavigableString,可導(dǎo)航的字符串"""
from bs4.element import NavigableString
print(type(soup.title.string))   # 標(biāo)簽下的文本數(shù)據(jù)
"""beautifulSoup,bs對(duì)象"""
print(type(soup))
""" Comment:注釋"""
html = "<b><!--好好學(xué)習(xí),天天向上--></b>"
soup2 = BeautifulSoup(html, 'html.parser')
print(soup2.b.string, type(soup2.b.string))

遍歷文檔樹

遍歷子節(jié)點(diǎn)

  • contents 返回的是一個(gè)所有子節(jié)點(diǎn)的列表(了解)
  • children 返回的是一個(gè)子節(jié)點(diǎn)的迭代器(了解)
  • descendants 返回的是一個(gè)生成器遍歷子子孫孫(了解)
  • string 獲取標(biāo)簽里面的內(nèi)容(掌握)
  • strings 返回是一個(gè)生成器對(duì)象用過(guò)來(lái)獲取多個(gè)標(biāo)簽內(nèi)容(掌握)
  • stripped_strings 和strings 基本一致 但是它可以把多余的空格去掉(掌握)

遍歷父節(jié)點(diǎn)

  • parent 直接獲得父節(jié)點(diǎn)
  • parents 獲取所有的父節(jié)點(diǎn)

遍歷兄弟節(jié)點(diǎn)

  • next_sibling,下一個(gè)兄弟結(jié)點(diǎn)
  • previous_sibling,上一個(gè)兄弟結(jié)點(diǎn)
  • next_siblings,下一個(gè)所有兄弟結(jié)點(diǎn)
  • previous_siblings,上一個(gè)所有兄弟結(jié)點(diǎn)

代碼實(shí)例

from bs4 import BeautifulSoup
html_doc = """
<html>
<head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">
Once upon a time there were three little sisters; and their names were
<a  class="sister" id="link1">Elsie</a>,
<a  class="sister" id="link2">Lacie</a> and
<a  class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'lxml')
r1 = soup.title.string  # 獲取單個(gè)標(biāo)簽里面的內(nèi)容  The Dormouse's story
# 獲取html中所有的標(biāo)簽中的內(nèi)容
r2 = soup.html.strings  # 返回是一個(gè)生成 generator對(duì)象,用過(guò)來(lái)獲取多個(gè)標(biāo)簽內(nèi)容
for i in r2:
    print(i)
r3 = soup.html.stripped_strings  # 獲取html中所有的標(biāo)簽中的內(nèi)容,并去掉多余的空格
for i in r3:
    print("---", i)

搜索文檔樹

  • find():返回搜索到的第一條數(shù)據(jù);
  • find_all():以列表形式返回所有的搜索到的標(biāo)簽數(shù)據(jù);

代碼實(shí)例

from bs4 import BeautifulSoup
html = """
<table class="tablelist" cellpadding="0" cellspacing="0">
    <tbody>
        <tr class="h">
            <td class="l" width="374">職位名稱</td>
            <td>職位類別</td>
            <td>人數(shù)</td>
            <td>地點(diǎn)</td>
            <td>發(fā)布時(shí)間</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=33824&keywords=python&tid=87&lid=2218">22989-金融云區(qū)塊鏈高級(jí)研發(fā)工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=29938&keywords=python&tid=87&lid=2218">22989-金融云高級(jí)后臺(tái)開發(fā)</a></td>
            <td>技術(shù)類</td>
            <td>2</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31236&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂(lè)運(yùn)營(yíng)開發(fā)工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>2</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31235&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂(lè)業(yè)務(wù)運(yùn)維工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=34531&keywords=python&tid=87&lid=2218">TEG03-高級(jí)研發(fā)工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=34532&keywords=python&tid=87&lid=2218">TEG03-高級(jí)圖像算法研發(fā)工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31648&keywords=python&tid=87&lid=2218">TEG11-高級(jí)AI開發(fā)工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>4</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=32218&keywords=python&tid=87&lid=2218">15851-后臺(tái)開發(fā)工程師</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=32217&keywords=python&tid=87&lid=2218">15851-后臺(tái)開發(fā)工程師</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a id="test" class="test" target='_blank' href="position_detail.php?id=34511&keywords=python&tid=87&lid=2218">SNG11-高級(jí)業(yè)務(wù)運(yùn)維工程師(深圳)</a></td>
            <td>技術(shù)類</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
    </tbody>
</table>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup, type(soup))
# 獲取所有的 tr 標(biāo)簽
trs = soup.find_all("tr")
for tr in trs:
    print(tr)
    print('*' * 30)
# 獲取第二個(gè) tr 標(biāo)簽
tr = soup.find_all('tr')[1]
print(tr)
# 排除第一個(gè)tr值(通過(guò)切片的方式)
jobMsg = soup.find_all('tr')[1:]
print(jobMsg)
# 獲取所有的 class = even 的 tr 標(biāo)簽:
# trs = soup.find_all('tr', class_='even')  # class為關(guān)鍵字,不能直接用作變量名
trs = soup.find_all('tr', attrs={"class": "even"})  # 效果同上,如果有多個(gè)值,在后面添加即可,推薦
for tr in trs:
    print(tr)
    print('--' * 44)
# 獲取所有a標(biāo)簽里面的 href 屬性值:
allA = soup.find_all('a')
for a in allA:
    href = a.get('href')
    print(href)
# 獲取所有的崗位信息
trs = soup.find_all('tr')[1:]   # 把第一個(gè)的表頭去掉
# print(trs)
for tr in trs:
    tds = tr.find_all('td')   # 找到所有的 td
    jobName = tds[0].string   # 獲取文本數(shù)據(jù),如果數(shù)據(jù)狠多,可以使用strings
    print(jobName)

以上就是Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python BeautifulSoup4的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論