【Python基礎(chǔ)】Python爬蟲的兩套解析方法和四種信息提取方式
Python爬蟲
Author:Iouwill
Machine?Learning Lab
分享一篇往日舊文章,非常實用。
? ? ?對于大多數(shù)朋友而言,爬蟲絕對是學(xué)習(xí)python的最好的起手和入門方式。因為爬蟲思維模式固定,編程模式也相對簡單,一般在細節(jié)處理上積累一些經(jīng)驗都可以成功入門。本文想針對某一網(wǎng)頁對python基礎(chǔ)爬蟲的兩大解析庫(BeautifulSoup和lxml)和幾種信息提取實現(xiàn)方法進行分析,以開python爬蟲之初見。
基礎(chǔ)爬蟲的固定模式
????筆者這里所談的基礎(chǔ)爬蟲,指的是不需要處理像異步加載、驗證碼、代理等高階爬蟲技術(shù)的爬蟲方法。一般而言,基礎(chǔ)爬蟲的兩大請求庫urllib和requests中requests通常為大多數(shù)人所鐘愛,當(dāng)然urllib也功能齊全。兩大解析庫BeautifulSoup因其強大的HTML文檔解析功能而備受青睞,另一款解析庫lxml在搭配xpath表達式的基礎(chǔ)上也效率提高。就基礎(chǔ)爬蟲來說,兩大請求庫和兩大解析庫的組合方式可以依個人偏好來選擇。
????筆者喜歡用的爬蟲組合工具是:
requests+BeautifulSouprequests+lxml
同一網(wǎng)頁爬蟲的四種實現(xiàn)方式
? ??筆者以騰訊新聞首頁的新聞信息抓取為例。
????首頁外觀如下:

????比如說我們想抓取每個新聞的標(biāo)題和鏈接,并將其組合為一個字典的結(jié)構(gòu)打印出來。首先查看HTML源碼確定新聞標(biāo)題信息組織形式。

????可以目標(biāo)信息存在于em標(biāo)簽下a標(biāo)簽內(nèi)的文本和href屬性中??芍苯永?/span>requests庫構(gòu)造請求,并用BeautifulSoup或者lxml進行解析。
方式一:
requests+BeautifulSoup+selectcss選擇器
# select method
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}url = 'http://news.qq.com/'
Soup = BeautifulSoup(requests.get(url=url, headers=headers).text.encode("utf-8"), 'lxml')em = Soup.select('em[class="f14 l24"] a')
for i in em: ? ?title = i.get_text() ? ?link = i['href'] ? ?print({'標(biāo)題': title,
? ? ? ? ? '鏈接': link ? ?})????很常規(guī)的處理方式,抓取效果如下:

方式二:
requests+BeautifulSoup+find_all進行信息提取
# find_all method
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}url = 'http://news.qq.com/'
Soup = BeautifulSoup(requests.get(url=url, headers=headers).text.encode("utf-8"), 'lxml')em = Soup.find_all('em', attrs={'class': 'f14 l24'})for i in em: ? ?title = i.a.get_text() ? ?link = i.a['href'] ? ?print({'標(biāo)題': title,
? ? ? ? ? '鏈接': link ? ?})????同樣是requests+BeautifulSoup的爬蟲組合,但在信息提取上采用了find_all的方式。效果如下:

方式三:
requests+lxml/etree+xpath表達式
# lxml/etree method
import requests
from lxml import etreeheaders = { ? ?'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}url = 'http://news.qq.com/'
html = requests.get(url = url, headers = headers)con = etree.HTML(html.text)title = con.xpath('//em[@class="f14 l24"]/a/text()')link = con.xpath('//em[@class="f14 l24"]/a/@href')
for i in zip(title, link): ? ?print({'標(biāo)題': i[0],
? ? ? ? ? '鏈接': i[1] ? ?})????使用lxml庫下的etree模塊進行解析,然后使用xpath表達式進行信息提取,效率要略高于BeautifulSoup+select方法。這里對兩個列表的組合采用了zip方法。效果如下:

方式四:
requests+lxml/html/fromstring+xpath表達式
# lxml/html/fromstring method
import requests
import lxml.html as HTMLheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}url = 'http://news.qq.com/'
con = HTML.fromstring(requests.get(url = url, headers = headers).text)title = con.xpath('//em[@class="f14 l24"]/a/text()')link = con.xpath('//em[@class="f14 l24"]/a/@href')
for i in zip(title, link): ? ?print({'標(biāo)題': i[0],'鏈接': i[1] ? ?})????跟方法三類似,只是在解析上使用了lxml庫下的html.fromstring模塊。抓取效果如下:

????很多人覺得爬蟲有點難以掌握,因為知識點太多,需要懂前端、需要python熟練、還需要懂?dāng)?shù)據(jù)庫,更不用說正則表達式、XPath表達式這些。其實對于一個簡單網(wǎng)頁的數(shù)據(jù)抓取,不妨多嘗試幾種抓取方案,舉一反三,也更能對python爬蟲有較深的理解。長此以往,對于各類網(wǎng)頁結(jié)構(gòu)都有所涉獵,自然經(jīng)驗豐富,水到渠成。
往期精彩回顧
獲取一折本站知識星球優(yōu)惠券,復(fù)制鏈接直接打開:
https://t.zsxq.com/662nyZF
本站qq群1003271085。
加入微信群請掃碼進群(如果是博士或者準(zhǔn)備讀博士請說明):
