PySpider 簡介
PySpider是一個國人編寫的強大的網絡爬蟲系統并帶有強大的WebUI。采用Python語言編寫,分布式架構,支持多種數據庫后端,強大的WebUI支持腳本編輯器、任務監視器,項目管理器以及結果查看器。
PySpider 來源于以前做的一個垂直搜索引擎使用的爬蟲后端。我們需要從200個站點(由于站點失效,不是都同時啦,同時有100+在跑吧)采集數據,并要求在5分鐘內將對方網站的更新更新到庫中。所以,靈活的抓取控制是必須的。
同時,由于100個站點,每天都可能會有站點失效或者改版,所以需要能夠監控模板失效,以及查看抓取狀態。
為了達到5分鐘更新,我們使用抓取最近更新頁上面的最后更新時間,以此來判斷頁面是否需要再次抓取。
可見,這個項目對于爬蟲的監控和調度要求是非常高的。
PySpider 中文網:http://www.pyspider.cn
PySpider 官網:http://docs.pyspider.org
PySpider 演示:http://demo.pyspider.org
PySpider 源碼:https://github.com/binux/pyspider
PySpider 特性
python 腳本控制,可以用任何你喜歡的html解析包(內置 pyquery)
WEB 界面編寫調試腳本、起停腳本、監控執行狀態,查看活動歷史,獲取結果產出
數據存儲支持MySQL、MongoDB、Redis、SQLite、 ElasticSearch; PostgreSQL 及 SQLAlchemy
隊列服務支持RabbitMQ、Beanstalk、Redis、Kombu
支持抓取 JavaScript 的頁面
組件可替換,支持單機/分布式部署,支持 Docker 部署
強大的調度控制,支持超時重爬及優先級設置
支持Python 2.{6, 7}, 3.{3, 4, 5, 6}
PySpider 安裝
1) pip 安裝
pip可以在以下版本的CPython下運行:2.6, 2.7, 3.1, 3.2, 3.3, 3.4 和 pypy.
pip可以在Unix/Linux、Mac OS X、Windows系統中運行.
a)腳本安裝
python get-pip.py
如果 setuptools (或 distribute) 未安裝, get-pip.py
會 自動為你安裝 setuptools
如果需要升級 setuptools (或 distribute),運行 pip install -U setuptools
b)命令安裝
sudo apt-get install python-pip // Debian、Ubuntu
sudo yum install python-pip // CentOS、Redhat、Fedora
2)PhantomJS 安裝
PhantomJS 是一個基于 WebKit 的服務器端 JavaScript API。它全面支持web而不需瀏覽器支持,其快速、原生支持各種Web標準:DOM 處理、CSS 選擇器、JSON、Canvas 和 SVG。 PhantomJS 可以用于頁面自動化、網絡監測、網頁截屏以及無界面測試等。支持Windows、Linux、Mac OS X等多操作系統。
PhantomJS 下載:http://phantomjs.org/download.html
PhantomJS不需要安裝,解壓后,配置環境變量后,便可直接使用,詳見 PhantomJS 安裝與開發
PhantomJS 安裝命令:
sudo apt-get install phantomjs // Debian、Ubuntu
sudo pkg install phantomjs // FreeBSD
brew install phantomjs // Mac OS X
3)PySpider 安裝
PySpider 安裝的依賴包 requirements.txt
Flask>=0.10
Jinja2>=2.7
chardet>=2.2
cssselect>=0.9
lxml
pycurl
pyquery
requests>=2.2
tornado>=3.2
mysql-connector-python>=1.2.2
pika>=0.9.14
pymongo>=2.7.2
unittest2>=0.5.1
Flask-Login>=0.2.11
u-msgpack-python>=1.6
click>=3.3
SQLAlchemy>=0.9.7
six>=1.5.0
amqp>=1.3.0,<2.0
redis
redis-py-cluster
kombu
psycopg2
elasticsearch
tblib
PySpider 安裝命令:
pip install pyspider
Ubuntu 用戶,請提前安裝好以下支持類庫:
sudo apt-get install python python-dev python-distribute python-pip libcurl4-openssl-dev libxml2-dev libxslt1-dev python-lxml
4)驗證安裝成功
控制臺輸入命令:
pyspider all
使用瀏覽器訪問 http://localhost:5000
正常出現 PySpider 的頁面,那證明一切 OK
PySpider 示例
1)示例1:爬取米撲科技首頁(mimvp.com)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-07-28 13:44:53
# Project: pyspiderdemo
# mimvp.com
from pyspider.libs.base_handler import *
class Handler(BaseHandler):
crawl_config = {
}
@every(minutes=24 * 60)
def on_start(self):
self.crawl('mimvp.com', callback=self.index_page)
@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
self.crawl(each.attr.href, callback=self.detail_page)
@config(priority=2)
def detail_page(self, response):
return {
"url": response.url,
"title": response.doc('title').text(),
}
2)示例2:設置代理爬取網頁
PySpider 支持使用代理爬取網頁,其使用代理有兩種方式:
方式1:
--phantomjs-proxy TEXT phantomjs proxy ip:port
啟動命令例如:
pyspider --phantomjs-proxy "188.226.141.217:8080" all
方式2:
設置代理全局變量,如下圖:
crawl_config = { 'proxy' : '188.226.141.217:8080'}
示例代碼:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-07-28 14:13:14
# Project: mimvp_proxy_pyspider
#
# mimvp.com
from pyspider.libs.base_handler import *
class Handler(BaseHandler):
crawl_config = {
'proxy' : 'http://188.226.141.217:8080', # http
'proxy' : 'https://182.253.32.65:3128' # https
}
@every(minutes=24 * 60)
def on_start(self):
self.crawl('http://proxy.mimvp.com/exist.php', callback=self.index_page)
@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
self.crawl(each.attr.href, callback=self.detail_page)
@config(priority=2)
def detail_page(self, response):
return {
"url": response.url,
"title": response.doc('title').text(),
}
參考推薦:
PhantomJS 安裝與開發
Python requests 安裝與開發
Python scrapy 安裝與開發
Node.js 安裝與開發
Node.js SuperAgent 安裝與開發
Python3 urllib 用法詳解