讀者可能會奇怪我標題怎么理成這個鬼樣子,主要是單單寫 lxml 與 bs4 這兩個 py 模塊名可能並不能一下引起大眾的注意,一般講到網頁解析技術,提到的關鍵詞更多的是 BeautifulSoup 和 xpath ,而它們各自所在的模塊(python 中是叫做模塊,但其他平台下更多地是稱作庫),很少被拿到明面上來談論。下面我將從效率、復雜度等多個角度來對比 xpath 與 beautifulsoup 的區別。
效率
使用復雜度
def get_nav(self,response): # soup = BeautifulSoup(response.body_as_unicode(), 'lxml') # nav_list = soup.find('ul', id='nav').find_all('li') model = etree.HTML(response.body_as_unicode()) nav_list = model.xpath('//ul[@id="nav"]/li') for nav in nav_list[1:]: # href = nav.find('a').get('href') href = nav.xpath('./a/@href')[0] yield Request(href, callback=self.get_url)
可以看到 xpath 除了其特殊的語法看上去有些別扭(跟正則表達式似的)以外,它在代碼簡潔度上還是可觀的,只是所有 xpath 方法的返回結果都是一個 list ,如果匹配目標是單個元素,對於無腦下標取0的操作,強迫症患者可能有些難受。相比之下,BeautifulSoup 這一長串的 find 與 find_all 方法顯得有些呆板,如果碰到搜索路線比較曲折的,比如:
# href = article.find('div', class_='txt').find('p', class_='tit blue').find('span').find('em').find('a').get('href') href = article.xpath('./div[@class="txt"]//p[@class="tit blue"]/span/em/a/@href')[0]
這種情況下,BeautifulSoup 的寫法就顯得有些讓人反胃了,當然一般情況下不會出現這么長的路徑定位。
功能缺陷總結——BeautifulSoup
BeautifulSoup 在使用上的一個短板,就是在嵌套列表中去匹配元素的時候會顯得很無力,下面是一個例子(具體網頁結構可根據 index_page 在瀏覽器打開進行審查):
class RankSpider(spider): name = 'PCauto_rank' index_page = 'http://price.pcauto.com.cn/top/hot/s1-t1.html' api_url = 'http://price.pcauto.com.cn%s' def start_requests(self): yield Request(self.index_page, callback=self.get_left_nav) # 測試 BeautifulSoup 是否能連續使用兩個 find_all 方法 def get_left_nav(self,response): # model = etree.HTML(response.body_as_unicode()) # nav_list = model.xpath('//div[@id="leftNav"]/ul[@class="pb200"]/li//a[@class="dd "]') soup = BeautifulSoup(response.body_as_unicode(), 'lxml') nav_list = soup.find('div', id='leftNav').find('ul', class_='pb200').find_all('li').find_all('a', class_='dd') for sub_nav in nav_list: href = self.api_url % sub_nav.xpath('./@href')[0] yield Request(href, callback=self.get_url) def get_url(self): pass
使用注釋部分的 xpath 寫法沒什么問題,可實現准確定位,但用到 BeautifulSoup 去實現相應邏輯的時候,就要連續使用兩個 find_all 方法 ,顯然這種寫法不符合規范,運行的時候會報 AttributeError: 'ResultSet' object has no attribute 'find_all' 錯誤,這時候我們要實現這種匹配,只能先去遍歷各個 li ,然后調 find_all 方法找到 li 下的各個 a 標簽,實在繁瑣,所以這種場景用 xpath 來解決會省下不少麻煩。
當然這里我只是單單為了詮釋這么個問題才在故意在拿目標 url 時分這么多級的,實際開發中我這里用的是:
# nav_list = model.xpath('//div[@id="leftNav"]///a[@class="dd "]') nav_list = soup.find('div', id='leftNav').find_all('a', class_='dd')
但如果說我們的目標不是所有的 li 下面的 a 標簽,而是部分 class="*" 的 li 下面的 a 標簽,這時候我們就只能選擇使用 xpath 來達到目的,當然如果你喜歡寫遍歷,覺得這樣寫出來邏輯展示更清晰,那你可以跳過這一節。
功能缺陷總結——xpath
model = etree.HTML(response.body_as_unicode())
model.xpath('//div[@class="box box-2 box-4"]')
model.xpath('//div[@class="box box-2 box-4 mt25"]') model.xpath('//div[@class="box box-2 box-4 mt17"]')
來匹配目標,這可能要歸結於 xpath 在設計的時候本身就是以類名的完全匹配來確定目標的,哪怕多一個空格:
model.xpath('//a[@class="dd"]')
文本獲取
xpath 目標結點的 text 屬性值對應的只是當前匹配元素下面的文本信息,如要獲取該結點下面包括子結點在內的所有文本內容要使用 .xpath('string()') 的方式:
model = etree.HTML(response.body_as_unicode())
place = model.xpath('//div[@class="guide"]')
# nav and aiticle if place: mark = place[0].xpath('./span[@class="mark"]') if mark: # text = mark[0].text.strip().replace('\n','').replace('\r','') # false text = mark[0].xpath('string()') result['address'] = text
其他方面比較