按照上一篇文章中我们将代码放到远程主机是通过拷贝或者git的方式,但是如果考虑到我们又多台远程主机的情况,这种方式就比较麻烦,那有没有好用的方法呢?这里其实可以通过scrapyd,下面是这个scrapyd的github地址:https://github.com/scrapy/scrapyd
当在远程主机上安装了scrapyd并启动之后,就会再远程主机上启动一个web服务,默认是6800端口,这样我们就可以通过http请求的方式,通过接口的方式管理我们scrapy项目,这样就不需要在一个一个电脑连接拷贝过着通过git,关于scrapyd官方文档地址:http://scrapyd.readthedocs.io/en/stable/
安装scrapyd
安装scrapyd:pip install scrapyd
这里我在另外一台ubuntu linux虚拟机中同样安装scrapy以及scrapyd等包,保证所要运行的爬虫需要的包都完成安装,这样我们就有了两台linux,包括上篇文章中我们已经有的linux环境
在这里有个小问题需要注意,默认scrapyd启动是通过scrapyd就可以直接启动,这里bind绑定的ip地址是127.0.0.1端口是:6800,这里为了其他虚拟机访问讲ip地址设置为0.0.0.0
scrapyd的配置文件:/usr/local/lib/python3.5/dist-packages/scrapyd/default_scrapyd.conf
这样我们就可以通过浏览器访问:
关于部署
如何通过scrapyd部署项目,这里官方文档提供一个地址:https://github.com/scrapy/scrapyd-client,即通过scrapyd-client进行操作
这里的scrapyd-client主要实现以下内容:
- 把我们本地代码打包生成egg文件
- 根据我们配置的url上传到远程服务器上
我们将我们本地的scrapy项目中scrapy.cfg配置文件进行配置:
我们其实还可以设置用户名和密码,不过这里没什么必要,只设置了url
这里设置url一定要注意:url = http://192.168.1.9:6800/addversion.json
最后的addversion.json不能少
我们在本地安装pip install scrapy_client,安装完成后执行:scrapyd-deploy
zhaofandeMBP:zhihu_user zhaofan$ scrapyd-deploy
Packing version
Deploying to project "zhihu_user" in http://192.168.1.9:6800/addversion.json
Server response ():
{"node_name": "fan-VirtualBox", "status": "ok", "version": "", "spiders": , "project": "zhihu_user"} zhaofandeMBP:zhihu_user zhaofan$
看到status:200表示已经成功
关于常用操作API
listprojects.json列出上传的项目列表
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/listprojects.json
{"node_name": "fan-VirtualBox", "status": "ok", "projects": ["zhihu_user"]}
zhaofandeMBP:zhihu_user zhaofan$
listversions.json列出有某个上传项目的版本
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/listversions.json\?project\=zhihu_user
{"node_name": "fan-VirtualBox", "status": "ok", "versions": [""]}
zhaofandeMBP:zhihu_user zhaofan$
schedule.json远程任务的启动
下面我们启动的三次就表示我们启动了三个任务,也就是三个调度任务来运行zhihu这个爬虫
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/schedule.json -d project=zhihu_user -d spider=zhihu
{"node_name": "fan-VirtualBox", "status": "ok", "jobid": "97f1b5027c0e11e7b07a080027bbde73"}
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/schedule.json -d project=zhihu_user -d spider=zhihu
{"node_name": "fan-VirtualBox", "status": "ok", "jobid": "99595aa87c0e11e7b07a080027bbde73"}
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/schedule.json -d project=zhihu_user -d spider=zhihu
{"node_name": "fan-VirtualBox", "status": "ok", "jobid": "9abb1ba27c0e11e7b07a080027bbde73"}
zhaofandeMBP:zhihu_user zhaofan$
同时当启动完成后,我们可以通过页面查看jobs,这里因为我远端服务器并没有安装scrapy_redis,所以显示任务是完成了,我点开日志并能看到详细的日志情况:
这里出错的原因就是我上面忘记在ubuntu虚拟机安装scrapy_redis以及pymongo模块,进行
pip install scrapy_redis pymongo安装后重新启动,就可以看到已经在运行的任务,同时点开Log日志也能看到爬取到的内容:
listjobs.json列出所有的jobs任务
上面是通过页面显示所有的任务,这里是通过命令获取结果
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/listjobs.json\?project\=zhihu_user
{"node_name": "fan-VirtualBox", "status": "ok", "running": [], "pending": [], "finished": [{"start_time": "2017-08-08 15:53:00.510050", "spider": "zhihu", "id": "97f1b5027c0e11e7b07a080027bbde73", "end_time": "2017-08-08 15:53:01.416139"}, {"start_time": "2017-08-08 15:53:05.509337", "spider": "zhihu", "id": "99595aa87c0e11e7b07a080027bbde73", "end_time": "2017-08-08 15:53:06.627125"}, {"start_time": "2017-08-08 15:53:10.509978", "spider": "zhihu", "id": "9abb1ba27c0e11e7b07a080027bbde73", "end_time": "2017-08-08 15:53:11.542001"}]}
zhaofandeMBP:zhihu_user zhaofan$
cancel.json取消所有运行的任务
这里可以将上面启动的所有jobs都可以取消:
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/cancel.json -d project=zhihu_user -d job=0f5cdabc7c1011e7b07a080027bbde73
{"node_name": "fan-VirtualBox", "status": "ok", "prevstate": "running"}
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/cancel.json -d project=zhihu_user -d job=63f8e12e7c1011e7b07a080027bbde73
{"node_name": "fan-VirtualBox", "status": "ok", "prevstate": "running"}
zhaofandeMBP:zhihu_user zhaofan$ curl http://192.168.1.9:6800/cancel.json -d project=zhihu_user -d job=63f8e12f7c1011e7b07a080027bbde73
{"node_name": "fan-VirtualBox", "status": "ok", "prevstate": "running"}
这样当我们再次通过页面查看,就可以看到所有的任务都是finshed状态:
我相信看了上面这几个方法你一定会觉得真不方便还需要输入那么长,所以有人替你干了件好事把这些API进行的再次封装:https://github.com/djm/python-scrapyd-api
关于python-scrapyd-api
该模块可以让我们直接在python代码中进行上述那些api的操作
首先先安装该模块:pip install python-scrapyd-api
使用方法如下,这里只演示了简单的例子,其他方法其实使用很简单按照规则写就行:
from scrapyd_api import ScrapydAPI scrapyd = ScrapydAPI('http://192.168.1.9:6800')
res = scrapyd.list_projects()
res2 = scrapyd.list_jobs('zhihu_user')
print(res)
print(res2)
Cancel a scheduled job
scrapyd.cancel('project_name', '14a6599ef67111e38a0e080027880ca6')
Delete a project and all sibling versions
scrapyd.delete_project('project_name')
Delete a version of a project
scrapyd.delete_version('project_name', 'version_name')
Request status of a job
scrapyd.job_status('project_name', '14a6599ef67111e38a0e080027880ca6')
List all jobs registered
scrapyd.list_jobs('project_name')
List all projects registered
scrapyd.list_projects()
List all spiders available to a given project
scrapyd.list_spiders('project_name')
List all versions registered to a given project
scrapyd.list_versions('project_name')
Schedule a job to run with a specific spider
scrapyd.schedule('project_name', 'spider_name')
Schedule a job to run while passing override settings
settings = {'DOWNLOAD_DELAY': 2}
Schedule a job to run while passing extra attributes to spider initialisation
scrapyd.schedule('project_name', 'spider_name', extra_attribute='value')