76
gooseeker集搜客
Python即時網絡爬蟲項目: 內容提取器的定義(Python2.7版本)
1. 項目背景
在Python 即時網絡爬蟲項目啟動說明中我們討論一個數字:程序員浪費在調測內容提取規則上的時間太多了(見頭圖),從而我們發起了這個項目,把程序員從繁瑣的調測規則中解放出來,投入到更高端的數據處理工作中。
這個項目推出以後受到很大關注,因為開放源碼,大家可以在現成源碼基礎上進一步開發。然而,Python3和Python2是有區別的,《Python即時網絡爬蟲項目: 內容提取器的定義》 一文的源碼無法在Python2.7下使用,本文將發布一個Python2.7的內容提取器。
2. 解決方案
為了解決這個問題,我們把影響通用性和工作效率的提取器隔離出來,描述了如下的數據處理流程圖:
圖中“可插拔提取器”必須很強的模塊化,那麼關鍵的接口有:
- 標準化的輸入:以標準的HTML DOM對象為輸入
- 標準化的內容提取:使用標準的xslt模板提取網頁內容
- 標準化的輸出:以標準的XML格式輸出從網頁上提取到的內容
- 明確的提取器插拔接口:提取器是一個明確定義的類,通過類方法與爬蟲引擎模塊交互
3. 提取器代碼
可插拔提取器是即時網絡爬蟲項目的核心組件,定義成一個類: GsExtractor
適用python2.7的源代碼文件及其說明文檔請從 github 下載
使用模式是這樣的:
- 實例化一個GsExtractor對象
- 為這個對象設定xslt提取器,相當於把這個對象配置好(使用三類setXXX()方法)
- 把html dom輸入給它,就能獲得xml輸出(使用extract()方法)
下麵是這個GsExtractor類的源代碼(適用於Python2.7)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 模塊名: gooseeker_py2
# 類名: GsExtractor
# Version: 2.0
# 適配Python版本: 2.7
# 說明: html內容提取器
# 功能: 使用xslt作為模板,快速提取HTML DOM中的內容。
# released by 集搜客(https://www.gooseeker.com) on May 18, 2016
# github: https://github.com/FullerHua/jisou/core/gooseeker_py2.py
from urllib2 import urlopen
from urllib import quote
from lxml import etree
import time
class GsExtractor(object):
def _init_(self):
self.xslt = ""
# 從文件讀取xslt
def setXsltFromFile(self , xsltFilePath):
file = open(xsltFilePath , 'r')
try:
self.xslt = file.read()
finally:
file.close()
# 從字符串獲得xslt
def setXsltFromMem(self , xsltStr):
self.xslt = xsltStr
# 通過GooSeeker API接口獲得xslt
def setXsltFromAPI(self , APIKey , theme, middle=None, bname=None):
apiurl = "https://www.gooseeker.com/api/getextractor?key="+ APIKey +"&theme="+quote(theme)
if (middle):
apiurl = apiurl + "&middle="+quote(middle)
if (bname):
apiurl = apiurl + "&bname="+quote(bname)
apiconn = urlopen(apiurl)
self.xslt = apiconn.read()
# 返回當前xslt
def getXslt(self):
return self.xslt
# 提取方法,入參是一個HTML DOM對象,返回是提取結果
def extract(self , html):
xslt_root = etree.XML(self.xslt)
transform = etree.XSLT(xslt_root)
result_tree = transform(html)
return result_tree
4. 用法示例
下麵是一個示例程序,演示怎樣使用GsExtractor類提取豆瓣討論組話題。本示例有如下特征:
- 提取器的內容通過GooSeeker平台上的api獲得
- 保存結果文件到當前文件夾
下麵是源代碼,都可從 github 下載
# _*_coding:utf8_*_
# douban_py2.py
# 爬取豆瓣小組討論話題
# Python版本: 2.7
from lxml import etree
from gooseeker_py2 import GsExtractor
from selenium import webdriver
import time
class PhantomSpider:
def getContent(self, url):
browser = webdriver.PhantomJS(executable_path='C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
browser.get(url)
time.sleep(3)
html = browser.execute_script("return document.documentElement.outerHTML")
output = etree.HTML(html)
return output
def saveContent(self, filepath, content):
file_obj = open(filepath, 'w')
file_obj.write(content)
file_obj.close()
doubanExtra = GsExtractor()
# 下麵這句調用gooseeker的api來設置xslt抓取規則
# 第一個參數是app key,請到GooSeeker會員中心申請
# 第二個參數是規則名,是通過GooSeeker的圖形化工具: 謀數台MS 來生成的
doubanExtra.setXsltFromAPI("ffd5273e213036d812ea298922e2627b" , "豆瓣小組討論話題")
url = "https://www.douban.com/group/haixiuzu/discussion?start="
totalpages = 5
doubanSpider = PhantomSpider()
print("爬取開始")
for pagenumber in range(1 , totalpages):
currenturl = url + str((pagenumber-1)*25)
print("正在爬取", currenturl)
content = doubanSpider.getContent(currenturl)
outputxml = doubanExtra.extract(content)
outputfile = "result" + str(pagenumber) +".xml"
doubanSpider.saveContent(outputfile , str(outputxml))
print("爬取結束")
提取結果如下圖所示:
5. 接下來閱讀
本文已經說明了提取器的價值和用法,但是沒有說怎樣生成它,隻有快速生成提取器才能達到節省開發者時間的目的,這個問題將在其他文章講解,請看《1分鍾快速生成用於網頁內容提取的xslt模板》
6. 集搜客GooSeeker開源代碼下載源
- GooSeeker開源Python網絡爬蟲GitHub源
7. 文檔修改曆史
- 2016-08-05:V1.0,Python2.7下的內容提取器類首次發布

最後更新:2017-01-09 14:08:09