Beautiful Soup庫

Beautiful Soup庫

安裝

  • win+X 命令提示符(使用管理員權(quán)限啟動控制臺)
  • 輸入安裝命令
pip install beautifulsoup4

Beautiful Soup庫的安裝小測

演示HTML頁面地址:http://python123.io/ws/demo.html

demo = r.text
from bs4 import BeautifulSoup
soup = BeautifulSoup(demo, "html.parser")
print(soup.prettify())

BeautifulSoup庫的基本元素

Beaufitul Soup庫的引用
Beautiful Soup庫,也叫beautifulsoup4或bs4

from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
soup = BeautifulSoup("<html>data</html>","html.parser")
soup2 = BeautifulSoup(open("D://demo.html"), "html.parser")

BeautifulSoup對應(yīng)一個HTML/XML文檔的全部內(nèi)容

Beautiful Soup庫解析器

解析器 使用方法 條件
bs4的HTML解析器 BeautifulSoup(mk, 'html.parser') 安裝bs4庫
lxml的HTML解析器 BeautifulSoup(mk, 'lxml') pip install lxml
lxml的XML解析器 BeautifulSoup(mk, 'xml') pip install lxml
html5lib的解析器 BeautifulSoup(mk, 'html5lib') pip install html5lib

Beautiful Soup類的基本元素

基本元素 說明
Tag 標(biāo)簽,最基本的信息組織黨員,分別用<>和</>標(biāo)明開頭和結(jié)尾
Name 標(biāo)簽的名字,<p>...</p>的名字是‘p’,格式:<tag>.name
Attributes 標(biāo)簽的屬性,字典形式組織,格式:<tag>.attrs
NavigableString 標(biāo)簽內(nèi)非屬性字符串,<>...</>中字符串,格式:<tag>.string
Comment 標(biāo)簽內(nèi)字符串的注釋部分,一種特殊的Comment類型

基于bs4庫的HTML內(nèi)容遍歷方法

  • 回顧demo.html
>>> import requests
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> demo
'<html><head><title>This is a python demo page</title></head><body><p class="title"><b>The demo python introduces several python courses.</b></p><p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a  class="py1" id="link1">Basic Python</a> and <a  class="py2" id="link2">Advanced Python</a>.</p></body></html>'
  • HTML基本格式
<html>
    <head>
        <title>This is a python demo page</title>
    </head>
    <body>
        <p class="title">
            <b>The demo python introduces several python courses.</b>
        </p>
        <p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
            <a  class="py1" id="link1">Basic Python</a>
             and 
            <a  class="py2" id="link2">Advanced Python</a>
            .
        </p>
    </body>
</html>

標(biāo)簽樹的下行遍歷

屬性 說明
.contents 子節(jié)點(diǎn)的列表,將<tag>所有兒子節(jié)點(diǎn)存入列表
.children 子節(jié)點(diǎn)的迭代類型,與.contents類似,用于循環(huán)遍歷兒子節(jié)點(diǎn)
.descendants 子孫節(jié)點(diǎn)的迭代類型,包含所有子孫節(jié)點(diǎn),用于循環(huán)遍歷
for child in soup.body.children:
    print(child)

標(biāo)簽樹的上行遍歷

屬性 說明
.parent 節(jié)點(diǎn)的父親標(biāo)簽
.parents 節(jié)點(diǎn)先輩標(biāo)簽的迭代類型,用于循環(huán)遍歷先輩節(jié)點(diǎn)
>>> soup = BeautifulSoup(demo, "html.parser")
>>> for parent in soup.a.parents:
           if parent is None:
               print(parent)
           else:
               print(parent.name)

標(biāo)簽樹的平行遍歷

屬性 說明
.next_sibling 返回按照HTML文本順序的下一個平行節(jié)點(diǎn)標(biāo)簽
.previous_sibling 返回按照HTML文本順序的上一個平行節(jié)點(diǎn)標(biāo)簽
.next_siblings 迭代類型,返回按照HTML文本順序的后續(xù)所有平行節(jié)點(diǎn)標(biāo)簽
.previous_siblings 迭代類型,返回按照HTML文本順序的前續(xù)所有平行節(jié)點(diǎn)標(biāo)簽
for sibling in soup.a.next_siblings:
    print(sibling)
for sibling in soup.a.previous_siblings:
    print(sibling)

基于bs4庫的HTML格式輸出

  • bs4庫的prettify()方法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Beautiful Soup庫是解析、遍歷、維護(hù)“標(biāo)簽樹”的功能庫 BeautifulSoup對應(yīng)一個HTML/X...
    紅茶紳士閱讀 640評論 0 0
  • 1.本周學(xué)習(xí)內(nèi)容思維導(dǎo)圖 2.Beautiful Soup 解析器 3.Beautiful Soup 類及其基本元...
    KelvinX閱讀 764評論 0 0
  • Beautiful Soup庫解析器 soup = BeautifulSoup(' data ','html.pa...
    NiceBlueChai閱讀 931評論 0 2
  • Beautiful Soup是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實(shí)...
    LitOrange閱讀 5,413評論 0 4
  • 體驗(yàn):路,很長,很陡,也許你走得很累!你想要放棄是很理所當(dāng)然,然而,你若是停住了前進(jìn)的腳步。路永遠(yuǎn)在前方,那里永遠(yuǎn)...
    九洋閱讀 196評論 0 0

友情鏈接更多精彩內(nèi)容