本節內容爲解析庫的使用,內容涵蓋:XPath、BeautifulSoup和PyQuery基礎內容。 html
from bs4 import BeautifulSoup soup = BeautifulSoup('<p>Hello</p>', 'lxml') #對象初始化 print(soup.p.string) #調用方法解析
from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml') print(soup.title.name) print(soup.p.attrs) #獲取屬性 print(soup.p.attrs['name']) print(soup.p.string) #獲取內容
#find_all() API find_all(name , attrs , recursive , text , **kwargs)python