上一篇文章的正則,其實對很多人來說用起來是不方便的,加上需要記很多規(guī)則,所以用起來不是特別熟練,而這節(jié)我們提到的beautifulsoup就是一個非常強大的工具,爬蟲利器。
beautifulSoup “美味的湯,綠色的濃湯”
一個靈活又方便的網(wǎng)頁解析庫,處理高效,支持多種解析器。
利用它就不用編寫正則表達式也能方便的實現(xiàn)網(wǎng)頁信息的抓取
快速使用
通過下面的一個例子,對bs4有個簡單的了解,以及看一下它的強大之處:
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'))
延伸閱讀
學習是年輕人改變自己的最好方式