1. 程式人生 > >Python爬蟲:如何建立BeautifulSoup物件

Python爬蟲:如何建立BeautifulSoup物件

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = '<div>text1</div>'
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
html = open('c:\\aa.html')

#以上三行表示了HTML的三種來源,一是字串形式,二是線上網頁形式,三是HTML檔案形式

bsObj = BeautifulSoup(html, 'html.parser') # 'html.parser'是解析器,也可以用'lxml'
# BeautifulSoup類似於C++中的建構函式 e = bsObj.find('div') print(e.text)