面向新手解析python Beautiful Soup基本用法
Beautiful Soup就是Python的一個HTML或XML的解析庫,可以用它來方便地從網(wǎng)頁中提取數(shù)據(jù)。它有如下三個特點:
- Beautiful Soup提供一些簡單的、Python式的函數(shù)來處理導航、搜索、修改分析樹等功能。它是一個工具箱,通過解析文檔為用戶提供需要抓取的數(shù)據(jù),因為簡單,所以不需要多少代碼就可以寫出一個完整的應用程序。
- Beautiful Soup自動將輸入文檔轉換為Unicode編碼,輸出文檔轉換為UTF-8編碼。你不需要考慮編碼方式,除非文檔沒有指定一個編碼方式,這時你僅僅需要說明一下原始編碼方式就可以了。
- Beautiful Soup已成為和lxml、html6lib一樣出色的Python解釋器,為用戶靈活地提供不同的解析策略或強勁的速度。
首先,我們要安裝它:pip install bs4,然后安裝 pip install beautifulsoup4.
Beautiful Soup支持的解析器

下面我們以lxml解析器為例:
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello</p>', 'lxml')
print(soup.p.string)
結果:
Hello
beautiful soup美化的效果實例:
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>, <a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and <a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml')#調(diào)用prettify()方法。這個方法可以把要解析的字符串以標準的縮進格式輸出 print(soup.prettify()) print(soup.title.string)
結果:
<html> <head> <title> The Dormouse's story </title> </head> <body> <p class="title" name="dromouse"> <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" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"> <!-- Elsie --> </a> , <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2"> Lacie </a> and <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p class="story"> ... </p> </body> </html> The Dormouse's story
下面舉例說明選擇元素、屬性、名稱的方法
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('輸出結果為title節(jié)點加里面的文字內(nèi)容:\n',soup.title)
print('輸出它的類型:\n',type(soup.title))
print('輸出節(jié)點的文本內(nèi)容:\n',soup.title.string)
print('結果是節(jié)點加其內(nèi)部的所有內(nèi)容:\n',soup.head)
print('結果是第一個p節(jié)點的內(nèi)容:\n',soup.p)
print('利用name屬性獲取節(jié)點的名稱:\n',soup.title.name)
#這里需要注意的是,有的返回結果是字符串,有的返回結果是字符串組成的列表。
# 比如,name屬性的值是唯一的,返回的結果就是單個字符串。
# 而對于class,一個節(jié)點元素可能有多個class,所以返回的是列表。
print('每個節(jié)點可能有多個屬性,比如id和class等:\n',soup.p.attrs)
print('選擇這個節(jié)點元素后,可以調(diào)用attrs獲取所有屬性:\n',soup.p.attrs['name'])
print('獲取p標簽的name屬性值:\n',soup.p['name'])
print('獲取p標簽的class屬性值:\n',soup.p['class'])
print('獲取第一個p節(jié)點的文本:\n',soup.p.string)
結果:
輸出結果為title節(jié)點加里面的文字內(nèi)容:
<title>The Dormouse's story</title>
輸出它的類型:
<class 'bs4.element.Tag'>
輸出節(jié)點的文本內(nèi)容:
The Dormouse's story
結果是節(jié)點加其內(nèi)部的所有內(nèi)容:
<head><title>The Dormouse's story</title></head>
結果是第一個p節(jié)點的內(nèi)容:
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
利用name屬性獲取節(jié)點的名稱:
title
每個節(jié)點可能有多個屬性,比如id和class等:
{'class': ['title'], 'name': 'dromouse'}
選擇這個節(jié)點元素后,可以調(diào)用attrs獲取所有屬性:
dromouse
獲取p標簽的name屬性值:
dromouse
獲取p標簽的class屬性值:
['title']
獲取第一個p節(jié)點的文本:
The Dormouse's story
在上面的例子中,我們知道每一個返回結果都是bs4.element.Tag類型,它同樣可以繼續(xù)調(diào)用節(jié)點進行下一步的選擇。
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('獲取了head節(jié)點元素,繼續(xù)調(diào)用head來選取其內(nèi)部的head節(jié)點元素:\n',soup.head.title)
print('繼續(xù)調(diào)用輸出類型:\n',type(soup.head.title))
print('繼續(xù)調(diào)用輸出內(nèi)容:\n',soup.head.title.string)
結果:
獲取了head節(jié)點元素,繼續(xù)調(diào)用head來選取其內(nèi)部的head節(jié)點元素: <title>The Dormouse's story</title> 繼續(xù)調(diào)用輸出類型: <class 'bs4.element.Tag'> 繼續(xù)調(diào)用輸出內(nèi)容: The Dormouse's story
(1)find_all()
find_all,顧名思義,就是查詢所有符合條件的元素。給它傳入一些屬性或文本,就可以得到符合條件的元素,它的功能十分強大。
find_all(name , attrs , recursive , text , **kwargs)
他的用法:
html='''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-small" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('查詢所有ul節(jié)點,返回結果是列表類型,長度為2:\n',soup.find_all(name='ul'))
print('每個元素依然都是bs4.element.Tag類型:\n',type(soup.find_all(name='ul')[0]))
#將以上步驟換一種方式,遍歷出來
for ul in soup.find_all(name='ul'):
print('輸出每個u1:',ul.find_all(name='li'))
#遍歷兩層
for ul in soup.find_all(name='ul'):
print('輸出每個u1:',ul.find_all(name='li'))
for li in ul.find_all(name='li'):
print('輸出每個元素:',li.string)
結果:
查詢所有ul節(jié)點,返回結果是列表類型,長度為2: [<ul class="list" id="list-1"> <li class="element">Foo</li> <li class="element">Bar</li> <li class="element">Jay</li> </ul>, <ul class="list list-small" id="list-2"> <li class="element">Foo</li> <li class="element">Bar</li> </ul>] 每個元素依然都是bs4.element.Tag類型: <class 'bs4.element.Tag'> 輸出每個u1: [<li class="element">Foo</li>, <li class="element">Bar</li>, <li class="element">Jay</li>] 輸出每個u1: [<li class="element">Foo</li>, <li class="element">Bar</li>] 輸出每個u1: [<li class="element">Foo</li>, <li class="element">Bar</li>, <li class="element">Jay</li>] 輸出每個元素: Foo 輸出每個元素: Bar 輸出每個元素: Jay 輸出每個u1: [<li class="element">Foo</li>, <li class="element">Bar</li>] 輸出每個元素: Foo 輸出每個元素: Bar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 使用Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作詳解
- Python爬蟲庫BeautifulSoup獲取對象(標簽)名,屬性,內(nèi)容,注釋
- Python下利用BeautifulSoup解析HTML的實現(xiàn)
- Python BeautifulSoup [解決方法] TypeError: list indices must be integers or slices, not str
- python中bs4.BeautifulSoup的基本用法
- Python HTML解析器BeautifulSoup用法實例詳解【爬蟲解析器】
- Python爬蟲beautifulsoup4常用的解析方法總結
- python爬蟲入門教程--HTML文本的解析庫BeautifulSoup(四)
相關文章
Python實現(xiàn)在PDF中添加數(shù)字簽名
無論是商業(yè)文件、法律文件還是個人文件,都可能需要證明其來源的真實性和完整性,PDF數(shù)字簽名就是解決這些問題的關鍵工具,下面我們來看看如何使用?Python?為PDF文檔添加數(shù)字簽名吧2025-01-01
在python win系統(tǒng)下 打開TXT文件的實例
下面小編就為大家分享一篇在python win系統(tǒng)下 打開TXT文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python 辦公自動化——基于pyqt5和openpyxl統(tǒng)計符合要求的名單
前幾天接到的一個需求,因為學校給的名單是青年大學習已學習的名單,然而要知道未學習的名單只能從所有團員中再排查一次,過程相當麻煩。剛好我也學過一些操作辦公軟件的基礎,再加上最近在學pyqt5,所以我決定用python寫個自動操作文件的腳本給她用用。2021-05-05
利用PyInstaller將python程序.py轉為.exe的方法詳解
這篇文章主要給大家介紹了利用PyInstaller將python程序.py轉為.exe的方法,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-05-05
Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報錯
這篇文章主要為大家介紹了Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報錯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

