搜尋
首頁後端開發Python教學python beautifulsoup4模組怎麼用

python beautifulsoup4模組怎麼用

May 11, 2023 pm 10:31 PM
pythonbeautifulsoup4

一、BeautifulSoup4 基礎知識補充

BeautifulSoup4 是一款python 解析庫,主要用於解析HTML 和XML,在爬蟲知識體系中解析HTML 會比較多一些,

此函式庫安裝指令如下:

pip install beautifulsoup4

BeautifulSoup 在解析資料時,需依賴第三方解析器,常用解析器與優點如下:

  • python 標準函式庫html.parser:python 內建標準函式庫,容錯能力強;

  • #lxml 解析器:速度快,容錯能力強;

  • #html5lib:容錯性最強,解析方式與瀏覽器一致。

接下來用一段自訂的HTML 程式碼來示範 beautifulsoup4 函式庫的基本使用,測試程式碼如下:

<html>
  <head>
    <title>测试bs4模块脚本</title>
  </head>
  <body>
    <h2 id="橡皮擦的爬虫课">橡皮擦的爬虫课</h2>
    <p>用一段自定义的 HTML 代码来演示</p>
  </body>
</html>

使用  BeautifulSoup 對其進行簡單的操作,包含實例化BS 對象,輸出頁標等內容。

from bs4 import BeautifulSoup
text_str = """<html>
	<head>
		<title>测试bs4模块脚本</title>
	</head>
	<body>
		<h2 id="橡皮擦的爬虫课">橡皮擦的爬虫课</h2>
		<p>用1段自定义的 HTML 代码来演示</p>
		<p>用2段自定义的 HTML 代码来演示</p>
	</body>
</html>
"""
# 实例化 Beautiful Soup 对象
soup = BeautifulSoup(text_str, "html.parser")
# 上述是将字符串格式化为 Beautiful Soup 对象,你可以从一个文件进行格式化
# soup = BeautifulSoup(open(&#39;test.html&#39;))
print(soup)
# 输入网页标题 title 标签
print(soup.title)
# 输入网页 head 标签
print(soup.head)

# 测试输入段落标签 p
print(soup.p) # 默认获取第一个

我們可以透過BeautifulSoup 對象,直接呼叫網頁標籤,這裡有一個問題,透過BS 物件呼叫標籤只能取得排在第一位置上的標籤,如上述程式碼中,只取得到了一個 p 標籤,如果想要取得更多內容,請繼續閱讀。

學習到這裡,我們需要了解BeautifulSoup 中的4 個內建物件:

  • BeautifulSoup:基本對象,整個HTML 對象,一般當做Tag 對像看即可;

  • Tag:標籤對象,標籤就是網頁中的各個節點,例如title,head,p;

  • NavigableString:標籤內部字串;

  • Comment:註解對象,爬蟲裡面使用場景不多。

下述程式碼為你示範這幾種物件出現的場景,注意程式碼中的相關註解:

from bs4 import BeautifulSoup
text_str = """<html>
	<head>
		<title>测试bs4模块脚本</title>
	</head>
	<body>
		<h2 id="橡皮擦的爬虫课">橡皮擦的爬虫课</h2>
		<p>用1段自定义的 HTML 代码来演示</p>
		<p>用2段自定义的 HTML 代码来演示</p>
	</body>
</html>
"""
# 实例化 Beautiful Soup 对象
soup = BeautifulSoup(text_str, "html.parser")
# 上述是将字符串格式化为 Beautiful Soup 对象,你可以从一个文件进行格式化
# soup = BeautifulSoup(open(&#39;test.html&#39;))
print(soup)
print(type(soup))  # <class &#39;bs4.BeautifulSoup&#39;>
# 输入网页标题 title 标签
print(soup.title)
print(type(soup.title)) # <class &#39;bs4.element.Tag&#39;>
print(type(soup.title.string)) # <class &#39;bs4.element.NavigableString&#39;>
# 输入网页 head 标签
print(soup.head)

對於Tag 物件,有兩個重要的屬性,是 name 和 attrs

from bs4 import BeautifulSoup
text_str = """<html>
	<head>
		<title>测试bs4模块脚本</title>
	</head>
	<body>
		<h2 id="橡皮擦的爬虫课">橡皮擦的爬虫课</h2>
		<p>用1段自定义的 HTML 代码来演示</p>
		<p>用2段自定义的 HTML 代码来演示</p>
		<a href="http://www.csdn.net" rel="external nofollow"  rel="external nofollow" >CSDN 网站</a>
	</body>
</html>
"""
# 实例化 Beautiful Soup 对象
soup = BeautifulSoup(text_str, "html.parser")
print(soup.name) # [document]
print(soup.title.name) # 获取标签名 title
print(soup.html.body.a) # 可以通过标签层级获取下层标签
print(soup.body.a) # html 作为一个特殊的根标签,可以省略
print(soup.p.a) # 无法获取到 a 标签
print(soup.a.attrs) # 获取属性

上述程式碼示範了取得  name 屬性和 attrs 屬性的用法,其中 attrs 屬性得到的是字典,可以透過鍵來取得對應的值。

取得標籤的屬性值,在BeautifulSoup 中,也可以使用以下方法:

print(soup.a["href"])
print(soup.a.get("href"))

取得 NavigableString 物件 取得了網頁標籤之後,就要取得標籤內文字了,透過下述程式碼進行。

print(soup.a.string)

除此之外,你還可以使用 text 屬性和 get_text() 方法來取得標籤內容。

print(soup.a.string)
print(soup.a.text)
print(soup.a.get_text())

也可以取得標籤內所有文本,使用 strings 和 stripped_strings 即可。

print(list(soup.body.strings)) # 获取到空格或者换行
print(list(soup.body.stripped_strings)) # 去除空格或者换行

擴展標籤/節點選擇器之遍歷文檔樹

#直接子節點

#標籤(Tag)物件的直接子元素,可以使用contents 和 children 屬性取得。

from bs4 import BeautifulSoup
text_str = """<html>
	<head>
		<title>测试bs4模块脚本</title>
	</head>
	<body>
		<div id="content">
			<h2 id="橡皮擦的爬虫课-span-最棒-span">橡皮擦的爬虫课<span>最棒</span></h2>
            <p>用1段自定义的 HTML 代码来演示</p>
            <p>用2段自定义的 HTML 代码来演示</p>
            <a href="http://www.csdn.net" rel="external nofollow"  rel="external nofollow" >CSDN 网站</a>
		</div>
        <ul class="nav">
            <li>首页</li>
            <li>博客</li>
            <li>专栏课程</li>
        </ul>

	</body>
</html>
"""
# 实例化 Beautiful Soup 对象
soup = BeautifulSoup(text_str, "html.parser")
# contents 属性获取节点的直接子节点,以列表的形式返回内容
print(soup.div.contents) # 返回列表
# children 属性获取的也是节点的直接子节点,以生成器的类型返回
print(soup.div.children) # 返回 <list_iterator object at 0x00000111EE9B6340>

請注意以上兩個屬性取得的都是直接子節點,例如 h2 標籤內的後代標籤 span ,不會單獨獲取到。

如果希望將所有的標籤都取得到,使用 descendants 屬性,它傳回的是一個產生器,所有標籤包含標籤內的文字都會單獨取得。

print(list(soup.div.descendants))

其它節點的獲取(了解即可,即查即用)

  • #parent 和 parents:直接父節點和所有父節點;

  • next_siblingnext_siblingsprevious_siblingprevious_siblings :分別表示下一個兄弟節點、下面所有兄弟節點、上一個兄弟節點、上面所有兄弟節點,由於換行符也是一個節點,所有在使用這幾個屬性時,請注意一下換行符;

  • next_elementnext_elementsprevious_elementprevious_elements:這幾個屬性分別表示上一個節點或下一個節點,注意它們不分層次,而是針對所有節點,例如上述程式碼中 div 節點的下一個節點是 h2,而 div 節點的兄弟節點是 ul

文件樹搜尋相關函數

第一個要學習的函數就是 find_all() 函數,原型如下圖所示:

find_all(name,attrs,recursive,text,limit=None,**kwargs)
  • name:此參數為tag 標籤的名字,例如 find_all('p') 是找出所有的 p 標籤,可接受標籤名稱字串、正規表示式與清單;

  • attrs:传入的属性,该参数可以字典的形式传入,例如 attrs={'class': 'nav'},返回的结果是 tag 类型的列表;

上述两个参数的用法示例如下:

print(soup.find_all(&#39;li&#39;)) # 获取所有的 li
print(soup.find_all(attrs={&#39;class&#39;: &#39;nav&#39;})) # 传入 attrs 属性
print(soup.find_all(re.compile("p"))) # 传递正则,实测效果不理想
print(soup.find_all([&#39;a&#39;,&#39;p&#39;])) # 传递列表
  • recursive:调用 find_all () 方法时,BeautifulSoup 会检索当前 tag 的所有子孙节点,如果只想搜索 tag 的直接子节点,可以使用参数 recursive=False,测试代码如下:

print(soup.body.div.find_all([&#39;a&#39;,&#39;p&#39;],recursive=False)) # 传递列表
  • text:可以检索文档中的文本字符串内容,与 name 参数的可选值一样,text 参数接受标签名字符串、正则表达式、 列表;

print(soup.find_all(text=&#39;首页&#39;)) # [&#39;首页&#39;]
print(soup.find_all(text=re.compile("^首"))) # [&#39;首页&#39;]
print(soup.find_all(text=["首页",re.compile(&#39;课&#39;)])) # [&#39;橡皮擦的爬虫课&#39;, &#39;首页&#39;, &#39;专栏课程&#39;]
  • limit:可以用来限制返回结果的数量;

  • kwargs:如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作 tag 的属性来搜索。这里要按 class 属性搜索,因为 class 是 python 的保留字,需要写作 class_,按 class_ 查找时,只要一个 CSS 类名满足即可,如需多个 CSS 名称,填写顺序需要与标签一致。

print(soup.find_all(class_ = &#39;nav&#39;))
print(soup.find_all(class_ = &#39;nav li&#39;))

还需要注意网页节点中,有些属性在搜索中不能作为kwargs参数使用,比如html5 中的 data-*属性,需要通过attrs参数进行匹配。

与 find_all()方法用户基本一致的其它方法清单如下:

  • find():函数原型find( name , attrs , recursive , text , **kwargs ),返回一个匹配元素;

  • find_parents(),find_parent():函数原型 find_parent(self, name=None, attrs={}, **kwargs),返回当前节点的父级节点;

  • find_next_siblings(),find_next_sibling():函数原型 find_next_sibling(self, name=None, attrs={}, text=None, **kwargs),返回当前节点的下一兄弟节点;

  • find_previous_siblings(),find_previous_sibling():同上,返回当前的节点的上一兄弟节点;

  • find_all_next(),find_next(),find_all_previous () ,find_previous ():函数原型 find_all_next(self, name=None, attrs={}, text=None, limit=None, **kwargs),检索当前节点的后代节点。

CSS 选择器 该小节的知识点与pyquery有点撞车,核心使用select()方法即可实现,返回数据是列表元组。

  • 通过标签名查找,soup.select("title")

  • 通过类名查找,soup.select(".nav")

  • 通过 id 名查找,soup.select("#content")

  • 通过组合查找,soup.select("div#content")

  • 通过属性查找,soup.select("div[id='content'")soup.select("a[href]")

在通过属性查找时,还有一些技巧可以使用,例如:

  • ^=:可以获取以 XX 开头的节点:

print(soup.select(&#39;ul[class^="na"]&#39;))
  • *=:获取属性包含指定字符的节点:

print(soup.select(&#39;ul[class*="li"]&#39;))

二、爬虫案例

BeautifulSoup 的基础知识掌握之后,在进行爬虫案例的编写,就非常简单了,本次要采集的目标网站 ,该目标网站有大量的艺术二维码,可以供设计大哥做参考。

python beautifulsoup4模組怎麼用

下述应用到了 BeautifulSoup 模块的标签检索与属性检索,完整代码如下:

from bs4 import BeautifulSoup
import requests
import logging
logging.basicConfig(level=logging.NOTSET)
def get_html(url, headers) -> None:
    try:
        res = requests.get(url=url, headers=headers, timeout=3)
    except Exception as e:
        logging.debug("采集异常", e)

    if res is not None:
        html_str = res.text
        soup = BeautifulSoup(html_str, "html.parser")
        imgs = soup.find_all(attrs={&#39;class&#39;: &#39;lazy&#39;})
        print("获取到的数据量是", len(imgs))
        datas = []
        for item in imgs:
            name = item.get(&#39;alt&#39;)
            src = item["src"]
            logging.info(f"{name},{src}")
            # 获取拼接数据
            datas.append((name, src))
        save(datas, headers)
def save(datas, headers) -> None:
    if datas is not None:
        for item in datas:
            try:
                # 抓取图片
                res = requests.get(url=item[1], headers=headers, timeout=5)
            except Exception as e:
                logging.debug(e)

            if res is not None:
                img_data = res.content
                with open("./imgs/{}.jpg".format(item[0]), "wb+") as f:
                    f.write(img_data)
    else:
        return None
if __name__ == &#39;__main__&#39;:
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
    }
    url_format = "http://www.9thws.com/#p{}"
    urls = [url_format.format(i) for i in range(1, 2)]
    get_html(urls[0], headers)

本次代码测试输出采用的 logging 模块实现,效果如下图所示。 测试仅采集了 1 页数据,如需扩大采集范围,只需要修改 main 函数内页码规则即可。 ==代码编写过程中,发现数据请求是类型是 POST,数据返回格式是 JSON,所以本案例仅作为 BeautifulSoup 的上手案例吧== 

python beautifulsoup4模組怎麼用

以上是python beautifulsoup4模組怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循環時:您什麼時候在Python中使用?循環時:您什麼時候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器