上一篇文章的正則,其實(shí)對(duì)很多人來(lái)說(shuō)用起來(lái)是不方便的,加上需要記很多規(guī)則,所以用起來(lái)不是特別熟練,而這節(jié)我們提到的beautifulsoup就是一個(gè)非常強(qiáng)大的工具,爬蟲(chóng)利器。
beautifulSoup “美味的湯,綠色的濃湯”
一個(gè)靈活又方便的網(wǎng)頁(yè)解析庫(kù),處理高效,支持多種解析器。
利用它就不用編寫正則表達(dá)式也能方便的實(shí)現(xiàn)網(wǎng)頁(yè)信息的抓取
快速使用
通過(guò)下面的一個(gè)例子,對(duì)bs4有個(gè)簡(jiǎn)單的了解,以及看一下它的強(qiáng)大之處:
from bs4 import BeautifulSoup html = '''<html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p>'''soup = BeautifulSoup(html,'lxml')print(soup.prettify())print(soup.title)print(soup.title.name)print(soup.title.string)print(soup.title.parent.name)print(soup.p)print(soup.p["class"])print(soup.a)print(soup.find_all('a'))print(soup.find(id='link3'))