关于爬虫平台的架构设计实现和框架的选型(二)--scrapy的内部实现以及实时爬虫的实现node
首先来看一下一个爬虫平台的设计,做为一个爬虫平台,须要支撑多种不一样的爬虫方式,因此通常爬虫平台须要包括python
一、 爬虫规则的维护,平台在接收到爬虫请求时,须要能按照匹配必定的规则去进行自动爬虫linux
二、 爬虫的job调度器,平台须要能负责爬虫任务的调度,好比定时调度,轮询调度等。数据库
三、 爬虫能够包括异步的海量爬虫,也能够包括实时爬虫,异步爬虫指的是爬虫的数据不会实时返回,可能一个爬虫任务会执行好久。 实时爬虫指爬的数据要实时返回,这个就要求时间很短,通常适合少许数据的爬虫。json
四、 爬虫好的数据能够生成指定的文件,好比csv文件,json文件等,而后经过数据处理引擎作统一处理,好比csv文件能够经过数据交换落入大数据平台,或者爬虫好的数据也能够丢入kafka中,而后再经过流式处理任务(spark或者storm,flink)作爬虫数据的清洗和处理,处理完的数据,能够入到数据库中。浏览器
下图就是在平台设计时,爬虫处理的一个流程,这个里面包含了实时爬虫,异步爬虫。bash
根据上图的处理流程,咱们能够把架构图进一步演进下架构
时序图以下:并发
咱们这里先介绍异步爬虫,爬虫的框架不少,异步爬虫通常用的比较多就是scrapy。
首先安装scrapy
pip install scrapy
安装完成后,就能够经过命令行建立一个基于scrapy的爬虫项目,咱们以爬取应用宝中理财类APP的名称为示例
建立爬虫项目的命令行命令:
scrapy startproject zj_scrapy
而后在命令行中,进入到建立的zj_scrapy目录下
cd zj_scrapy
执行
scrapy genspider sjqq “sj.qq.com”
建立一个爬虫
爬虫建立好了后,可使用IDE打开建立好的python项目,好比用idea(须要安装python插件,默认没有安装)打开咱们建立好的项目
项目建立好了后,会默认生成一些模板代码文件
一、 items.py
items用于存储字段的定义。即爬取的内容存与item类中,在这里咱们定义了一个name字段。
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class ZjScrapyItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() name = scrapy.Field() pass
二、 spider文件编写
这个文件通常在spiders 这个package下面,默认会继承scrapy.Spider
# -*- coding: utf-8 -*- import scrapy from scrapy.http import HtmlResponse from zj_scrapy.items import ZjScrapyItem class SjqqSpider(scrapy.Spider): name = 'sjqq' allowed_domains = ['sj.qq.com'] start_urls = ['https://sj.qq.com/myapp/category.htm?orgame=1&categoryId=114'] def parse(self, response:HtmlResponse): name_list = response.xpath('/html/body/div[3]/div[2]/ul/li') print("=============",response.headers) for each in name_list: item = ZjScrapyItem() name = each.xpath('./div/div/a[1]/text()').extract() item['name'] = name[0] yield item pass
关于这段代码的解释以下:
三、 pipeline文件编写
pipeline文件通常用于对处理好的爬虫结果数据作处理,能够入到数据库中,也能够生成到指定的文件中,process_item 方法就是对数据作处理的。
另外pipeline 还包含了__init__和close_spider 两个方法。__init__ 用于作初始化处理。 close_spider 用于执行结束时的操做处理。好比数据写入数据库或者文件后,对数据库作连接关闭或者文件流作关闭操做等。
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class ZjScrapyPipeline(object): def process_item(self, item, spider): print("+++++++++++++++++++",item['name']) print("-------------------",spider.cc) return item
四、 setting文件修改
setting文件中存放的是爬虫的配置,经常使用的配置通常能够包括
1)、ITEM_PIPELINES的配置,好比
ITEM_PIPELINES = {
'zj_scrapy.pipelines.ZjScrapyPipeline': 300,
}
这里的300表明了一个优先级,数值范围通常在0-1000,这个数值肯定了运行的顺序,数字越小,优先级越高。
2)、字符集配置,能够经过FEED_EXPORT_ENCODING指定字符集
FEED_EXPORT_ENCODING = 'utf-8'
3)、CONCURRENT_REQUESTS配置Scrapy执行的最大并发请求数
# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 32
4)配置请求的header,能够经过DEFAULT_REQUEST_HEADERS来进行配置
DEFAULT_REQUEST_HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
}
五、
本地执行爬虫
在上面的都作完后,能够经过执行命令行scrapy crawl sjqq -o items.csv 来在本地运行爬虫,sjqq 就是前面指定的爬虫名,-o items.csv 表示生成一个csv文件。
运行完成后,能够看到爬取的内容已经写到了指定的文件中。
在运行时,能够经过-a 指定自定义的参数,好比scrapy crawl sjqq -o items.csv -a cc=scrapttest
在这条执行命令中,咱们指定了一个cc参数等于scrapttest,在Pipeline
中,咱们能够经过代码获取这个参数
class ZjScrapyPipeline(object): def process_item(self, item, spider): print("+++++++++++++++++++",item['name']) print("-------------------",spider.cc) return item
在代码中,咱们经过spider.cc 就能够获取到这个参数的值,而后在运行日志能够看到,咱们打印出来了这个参数值。
经过这种方式,咱们就解决了爬虫运行时,参数的的动态传递问题。
六、
爬虫部署到服务端
安装scrapyd
pip install scrapyd
安装scrapyd-deploy
pip install scrapyd-client
scrapyd 是scrapy的爬虫服务端,安装完成后,执行scrapyd能够启动服务端。
启动时默认端口为6800
启动后,经过浏览器能够访问http://localhost:6800/
服务端启动后,就能够经过scrapyd-deploy 来提交部署开发好的爬虫了。
scrapyd-deploy <target> -p <project> --version <version>
部署成功后,就能够看到本身的爬虫项目了
七、
建立服务端的爬虫任务
若是是在linux命令下,能够经过
curl http://localhost:6800/schedule.json -d project= zj_scrapy -d spider=sjqq
来提交一个爬虫任务,提交完成后,会返回提交的任务状态,这个其实就是提交了一个http请求
{
"node_name": "ZJPH-0321",
"status": "ok",
"jobid": "dd7f10aca76e11e99b656c4b90156b7e"
}
提交成功后,能够在浏览器的job下面看到任务的执行状况
若是须要携带自定义的参数,那么能够经过-d来指定,好比-d cc=scrapttest,和前面在本地执行时,增长自定义参数是同样的。
也能够经过http请求工具(好比soapui)提交一个http请求来触发一个爬虫任务
schedule.json请求中还能够包含以下参数
setting (string, optional) –自定义爬虫settings
jobid (string, optional) –jobid,以前启动过的spider,会有一个id,这个是可选参数
_version (string, optional) –版本号,以前部署的时候的version,只能使用int数据类型,没指定,默认启动最新版本
八、 scrapyd 其余的API
1)、curl http://localhost:6800/daemonstatus.json 检查爬虫服务的状态
2)、addversion.json增长项目到服务端 若是项目已经存在,则增长一个新的版本
POST请求:
project (string, required) –项目名
version (string, required) –项目版本,不填写则是当前时间戳
egg (file, required) –当前项目的egg文件
curl http://localhost:6800/addversion.json -F project=myproject -F version=r23 -F egg=@myproject.egg
3)、 cancel.json
取消一个 spdier的运行
若是 spider是运行状态,则中止其运行
若是 spider是挂起状态,则删除spider
POST请求:
project (string, required) –项目名
job (string, required) -jobid
curl http://localhost:6800/cancel.json -d project=myproject -d job=6487ec79947edab326d6db28a2d86511e8247444
4)、listprojects.json
获取当前已上传的项目的列表
GET请求:
curl http://localhost:6800/listprojects.json
5)、listversions.json
获取指定项目的可用版本
GET请求:
project (string, required) –项目名
curl http://localhost:6800/listversions.json?project=myproject
6)、listspiders.json
获取指定版本的项目中的爬虫列表,若是没有指定版本,则是最新版本
GET请求:
project (string, required) –项目名
_version (string, optional) –版本号
$ curl http://localhost:6800/listspiders.json?project=myproject
7)、 listjobs.json
获取指定项目中全部挂起、运行和运行结束的job
GET请求
project (string, option) - restrict results to project name
curl http://localhost:6800/listjobs.json?project=myproject | python -m json.tool
8)、delversion.json
删除指定项目的指定版本
POST请求
project (string, required) - the project name
version (string, required) - the project version
curl http://localhost:6800/delversion.json -d project=myproject -d version=r99
9)、delproject.json
删除指定项目,而且包括全部的版本
POST请求
project (string, required) - the project name
curl http://localhost:6800/delproject.json -d project=myproject
未完待续 第二篇