小編給大家分享一下Python爬蟲中如何創(chuàng)建 beautifulsoup 對象,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
樂至ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
首先必須要導(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è)資訊頻道,感謝各位的閱讀!