小編給大家分享一下Python爬蟲中如何創(chuàng)建 beautifulsoup 對象,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)擁有網(wǎng)站維護技術(shù)和項目管理團隊,建立的售前、實施和售后服務(wù)體系,為客戶提供定制化的網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)站維護、綿陽服務(wù)器托管解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護服務(wù)覆蓋集團企業(yè)、上市公司、外企網(wǎng)站、成都做商城網(wǎng)站、政府網(wǎng)站等各類型客戶群體,為全球成百上千家企業(yè)提供全方位網(wǎng)站維護、服務(wù)器維護解決方案。首先必須要導(dǎo)入 bs4 庫
from bs4 import BeautifulSoup
我們創(chuàng)建一個字符串,后面的例子我們便會用它來演示
html = """The Dormouse's story The Dormouse's story
Once upon a time there were three little sisters; and their names were , Lacie and Tillie; and they lived at the bottom of a well.
...
"""
創(chuàng)建 beautifulsoup 對象
soup = BeautifulSoup(html)
另外,我們還可以用本地 HTML 文件來創(chuàng)建對象,例如
soup = BeautifulSoup(open('index.html'))
上面這句代碼便是將本地 index.html 文件打開,用它來創(chuàng)建 soup 對象 下面我們來打印一下 soup 對象的內(nèi)容,格式化輸出
print soup.prettify()
輸出結(jié)果:
The Dormouse's story The Dormouse's story
Once upon a time there were three little sisters; and their names were , Lacie and Tillie ; and they lived at the bottom of a well.
...
看完了這篇文章,相信你對Python爬蟲中如何創(chuàng)建 beautifulsoup 對象有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!