小編給大家分享一下Python爬蟲(chóng)中如何創(chuàng)建 beautifulsoup 對(duì)象,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站維護(hù)、綿陽(yáng)服務(wù)器托管解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、成都做商城網(wǎng)站、政府網(wǎng)站等各類(lèi)型客戶群體,為全球成百上千家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。首先必須要導(dǎo)入 bs4 庫(kù)
from bs4 import BeautifulSoup
我們創(chuàng)建一個(gè)字符串,后面的例子我們便會(huì)用它來(lái)演示
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p name="dromouse"><b>The Dormouse's story</b></p> <p>Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a href="http://example.com/lacie" id="link2">Lacie</a> and <a href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p>...</p> """
創(chuàng)建 beautifulsoup 對(duì)象
soup = BeautifulSoup(html)
另外,我們還可以用本地 HTML 文件來(lái)創(chuàng)建對(duì)象,例如
soup = BeautifulSoup(open('index.html'))
上面這句代碼便是將本地 index.html 文件打開(kāi),用它來(lái)創(chuàng)建 soup 對(duì)象 下面我們來(lái)打印一下 soup 對(duì)象的內(nèi)容,格式化輸出
print soup.prettify()
輸出結(jié)果:
<html> <head> <title> The Dormouse's story </title> </head> <body> <p name="dromouse"> <b> The Dormouse's story </b> </p> <p> Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" id="link1"> <!-- Elsie --> </a> , <a href="http://example.com/lacie" id="link2"> Lacie </a> and <a href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p> ... </p> </body> </html>
看完了這篇文章,相信你對(duì)Python爬蟲(chóng)中如何創(chuàng)建 beautifulsoup 對(duì)象有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞標(biāo)題:Python爬蟲(chóng)中如何創(chuàng)建beautifulsoup對(duì)象-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article44/dpgihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、標(biāo)簽優(yōu)化、網(wǎng)站收錄、App開(kāi)發(fā)、服務(wù)器托管、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容