347
gooseeker集搜客
為編寫網絡爬蟲程序安裝Python3.5
1. 下載Python3.5.1安裝包
1.1 進入python官網,點擊menu->downloads,網址:Download Python
1.2 根據係統選擇32位還是64位,這裏下載的可執行exe為64位安裝包
2. 安裝Python3.5
2.1 雙擊打開安裝包,選擇自定義路徑(注意安裝路徑中盡量不要含有有中文或者空格),然後選中Add Python 3.5 to PATH(將Python安裝路徑添加到係統變量Path中,這樣做以後在任意目錄下都可以執行pyhton命令了)
2.2 默認全選,Next
2.3 修改安裝路徑,勾選加上Install for all user為所有用戶安裝和Precompile standard library 預編譯標準庫,然後點擊Install
2.4 等待安裝完成
2.5 驗證,使用快捷鍵win + R 或 右鍵開始選擇運行,輸入cmd回車,打開命令提示符窗口,然後輸入python->回車,若出現python版本信息則軟件安裝完成
3. 簡單實踐,敲一個簡單小爬蟲程序
3.1 安裝lxml庫,由於直接使用pip lxml 對於3.0x以上的版本來說經常會出現版本不適應而失敗,所以這裏介紹直接使用whl文件安裝
3.1.1 下載對應python3.5版本的lxml庫,下載網址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml
3.1.2 同檢查python是否安裝成功一樣,使用快捷鍵win + R 或 右鍵開始選擇運行,輸入cmd回車,打開命令提示符窗口,然後
pip install E:\demo\lxml-3.6.4-cp35-cp35m-win_amd64.whl(下載的lxml庫whl文件存放路徑)
可能碰到問題,pip的版本低了,需要更新一下pip的版本。更新pip版本命令:
python -m pip install -U pip
更新完成後,再次使用pip命令:
pip install E:\demo\lxml-3.6.4-cp35-cp35m-win_amd64.whl
3.2 Lxml庫安裝成功後,環境就準備好了, 可以開始敲代碼了
3.2.1 引入Gooseeker規則提取器模塊gooseeker.py(引入該模塊的原因和價值),在自定義目錄下創建gooseeker.py文件,如:這裏為E:\Demo\gooseeker.py,再以記事本打開,複製下麵的代碼粘貼
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 模塊名: gooseeker
# 類名: GsExtractor
# Version: 2.0
# 說明: html內容提取器
# 功能: 使用xslt作為模板,快速提取HTML DOM中的內容。
# released by 集搜客(https://www.gooseeker.com) on May 18, 2016
# github: https://github.com/FullerHua/jisou/core/gooseeker.py
from urllib import request
from urllib.parse 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' , encoding='UTF-8')
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 = request.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
# 提取方法,入參是html源碼,返回是提取結果
def extractHTML(self , html):
doc = etree.HTML(html)
return self.extract(doc)
3.2.2 在提取器模塊gooseeker.py同級目錄下創建一個.py後綴文件,如這裏為E:\Demo\first.py,再以記事本打開,敲入代碼:
# -*- coding: utf-8 -*-
# 使用gsExtractor類的示例程序
# 訪問集搜客論壇,以xslt為模板提取論壇內容
# xslt保存在xslt_bbs.xml中
# 采集結果保存在result.xml中
import os
from urllib import request
from lxml import etree
from gooseeker import GsExtractor
# 訪問並讀取網頁內容
url = "https://www.gooseeker.com/cn/forum/7"
conn = request.urlopen(url)
doc = etree.HTML(conn.read())
bbsExtra = GsExtractor()
bbsExtra.setXsltFromAPI("31d24931e043e2d5364d03b8ff9cc77e" , "gooseeker_bbs_xslt") # 設置xslt抓取規則
result = bbsExtra.extract(doc) # 調用extract方法提取所需內容
# 當前目錄
current_path = os.getcwd()
file_path = current_path + "/result.xml"
# 保存結果
open(file_path,"wb").write(result)
# 打印出結果
print(str(result).encode('gbk','ignore').decode('gbk'))
3.2.3 執行first.py,使用快捷鍵win + R 或 右鍵開始選擇運行,輸入cmd回車,打開命令提示窗口,進入first.py文件所在目錄,輸入命令 :python first.py 回車
3.2.4 查看保存結果文件,進入first.py文件所在目錄,查看名稱為result的xml文件(即采集結果)
4. 總結
安裝步驟還是很簡單,主要需要注意的是:
- 對應係統版本安裝;
- 將安裝路徑加入係統環境變量Path。
後麵將會講到如何結合Scrapy快速開發Python爬蟲。
5. 集搜客GooSeeker開源代碼下載源
- GooSeeker開源Python網絡爬蟲GitHub源
6.相關文章
- 《Python即時網絡爬蟲項目啟動說明》
7. 文章修改曆史
- 2016-09-27:V1.0
- 2016-10-25:3.2.1補充代碼
若有疑問可以或
最後更新:2017-01-09 14:08:09