一    Scrapyd简介

Scrapyd 是一个用来部署和运行 Scrapy 项目的应用,由 Scrapy 的开发者开发。其可以通过一个简单的 Json API 来部署(上传)或者控制你的项目。

Scrapyd 可以用来管理多个项目,并且每个项目还可以上传多个版本,不过只有最新的版本会被使用。

在安装并开启 Scrapyd 之后,它将会挂起一个服务来监听运行爬虫的请求,并且根据请求为每一个爬虫启用一个进程来运行。Scrapyd 同样支持同时运行多个进程,进程的数量由max_proc 和 max_proc_per_cpu 选项来限制。

二 Scrapyd 安装部署

通常来说,使用 pip 安装 Scrapyd 即可:

$ pip install scrapyd

部署 scrapy

rsync -avz scrapyd crawler_server1:/data --exclude '*logs' --exclude '*.pyc' --exclude '*.db' --exclude '*env' --exclude '*eggs' --exclude '*.pid'

rsync -avz scrapyd crawler_server2:/data --exclude '*logs' --exclude '*.pyc' --exclude '*.db' --exclude '*env' --exclude '*eggs'

pip install -r requirements.txt

安装成功后使用以下命令即可以开启 Scrapyd 服务:

使用虚拟环境运行
virtualenv -p /usr/bin/python3.6 env
source env/bin/activate python scripts/scrapyd_run.py

三 scrapyd  配置文件

Scrapyd 将会在以下地址搜索配置文件,在解析的过程中更高的配置文件拥有更高的优先级:

  • /etc/scrapyd/scrapyd.conf (Unix)
  • c:\scrapyd\scrapyd.conf (Windows)
  • /etc/scrapyd/conf.d/* (in alphabetical order, Unix)
  • scrapyd.conf
  • ~/.scrapyd.conf (users home directory)

配置文件中支持配置以下选项:

  

  • http_port

    Scrapyd 的API监听的端口,默认为6800

  • bind_address

    网页和Json服务监听的IP地址,默认为 127.0.0.1

  • max_proc

    同时启动的最大Scrapy进程数,如果没有设置或者设置为0,那么将会使用当前cpu可用的核数乘以 max_proc_per_cpu 的值。默认为0

  • max_proc_per_cpu

    每个cpu能同时启动的最大Scrapy进程数。默认为4

  • debug

    是否开启debug模式,默认为off。开启之后,如果在调用Scrapy的Json API的时候出错,则会返回详细的traceback信息。

  • eggs_dir

    项目的eggs文件存储的目录。

  • dbs_dir

    项目存储数据库的目录,也包括爬虫队列。

  • logs_dir

    存储Scrapy日志的目录。如果不希望存储日志,那么需要设置成如下所示:

    logs_dir =
  • items_dir

    存储items的目录,一般来说不需要设置这个选项,因为抓取下来的数据都会存到数据库中。如果设置这个选项,那么将会覆盖Scrapy的FEED_URL设置,将抓取下来的items保存到指定目录。

  • jobs_to_keep

    每个spider保留多少个完成的job,默认为5。这更多指的是itemlog

  • finished_to_keep

    启动器中保留的已完成进程的数量,默认为100

  • poll_interval

    轮询队列的间隔,以秒为单位,默认值为5,可以为浮点数。

  • runner

    用来启动子进程的启动器,可以自定义启动的模块。

  • node_name

    每个节点的节点名称,默认为 ${socket.gethostname()}

以下是一个默认的配置文件的例子:

[scrapyd]
eggs_dir = eggs
logs_dir = logs
items_dir =
jobs_to_keep = 5
dbs_dir = dbs
max_proc = 0
max_proc_per_cpu = 4
finished_to_keep = 100
poll_interval = 5.0
bind_address = 127.0.0.1
http_port = 6800
debug = off
runner = scrapyd.runner
application = scrapyd.app.application
launcher = scrapyd.launcher.Launcher
webroot = scrapyd.website.Root [services]
schedule.json = scrapyd.webservice.Schedule
cancel.json = scrapyd.webservice.Cancel
addversion.json = scrapyd.webservice.AddVersion
listprojects.json = scrapyd.webservice.ListProjects
listversions.json = scrapyd.webservice.ListVersions
listspiders.json = scrapyd.webservice.ListSpiders
delproject.json = scrapyd.webservice.DeleteProject
delversion.json = scrapyd.webservice.DeleteVersion
listjobs.json = scrapyd.webservice.ListJobs
daemonstatus.json = scrapyd.webservice.DaemonStatus

四  配置访问认证

  由于 Scrapyd 本身不提供访问的认证,所以接口都是暴露在公共网络中的,需要使用 Nginx 配置反向代理等方法来设置访问认证。

五 API

  在开启了 scrapyd 服务之后,就可以通过 Scrapyd 提供的 API 接口来操作你的 Scrapy 项目了。

  daemonstatus.json

  用来检查服务器的负载状态。支持的请求方法:GET

  request例子:

curl http://localhost:6800/daemonstatus.json

  response例子:

{ "status": "ok", "running": "", "pending": "", "finished": "", "node_name": "node-name" }

  addversion.json

  给项目增加一个版本,如果项目不存在的话那么就创建一个项目。这个接口主要被用来上传项目或者更新项目版本。支持的请求方法:POST

  参数:

  • project (string, required) - 项目的名称
  • version (string, required) - 项目的版本
  • egg (file, required) - 一个包含项目代码的 egg 文件

  request例子:

$ curl http://localhost:6800/addversion.json -F project=myproject -F version=r23 -F egg=@myproject.egg

  相应例子:

{"status": "ok", "spiders": 3}

  schedule.json

  调度运行一个爬虫(也成为 job ),并返回一个 jobid。支持的请求方法:POST

  参数:

  • project (string, required) - 项目的名称
  • spider (string, required) - 爬虫的名称
  • setting (string, optional) - 爬虫运行时要用到的Scrapy设置
  • jobid (string, optional) - 用来作为标识的jobid,会覆盖掉默认生成的UUID
  • _version (string, optional) - 使用的项目版本
  • 任何其他的参数将会传递作为爬虫的参数

  request例子:

$ curl http://localhost:6800/schedule.json -d project=myproject -d spider=somespider

  response例子:

{"status": "ok", "jobid": "6487ec79947edab326d6db28a2d86511e8247444"}

  以下是传递 spider 参数和 setting 参数的例子:

$ curl http://localhost:6800/schedule.json -d project=myproject -d spider=somespider -d setting=DOWNLOAD_DELAY=2 -d arg1=val1

  cancel.json

  取消一个 job 的运行,如果这个 job 处于等待的状态,那么将会被移除,如果这个 job 正在运行,那么它将会被终止。支持的请求方法:POST

  参数:

  • project (string, required) - 项目的名称
  • job (string, required) - job的id

  request例子:

$ curl http://localhost:6800/cancel.json -d project=myproject -d job=6487ec79947edab326d6db28a2d86511e8247444

  response例子:

{"status": "ok", "prevstate": "running"}

  listprojects.json

  获取上传到服务器的项目列表。支持请求方法:GET,无参数。

  request例子:

$ curl http://localhost:6800/listprojects.json

  response例子:

{"status": "ok", "projects": ["myproject", "otherproject"]}

  listversions.json

  获取某个或某些项目的版本列表。返回的版本将会按顺序排列,最后的那个版本是当前正在使用的版本。支持请求参数:GET

  参数:

  • project (string, required) - 项目名称

  request例子:

$ curl http://localhost:6800/listversions.json?project=myproject

  response例子:

{"status": "ok", "versions": ["r99", "r156"]}

  listspiders.json

  默认获取某个项目最新版本中的spider列表。支持的请求方法:GET

  参数:

  • project (string, required) - 项目的名称
  • _version (string, optional) - 检查的项目版本

  request例子:

$ curl http://localhost:6800/listspiders.json?project=myproject

  response例子:

{"status": "ok", "spiders": ["spider1", "spider2", "spider3"]}

  listjobs.json

  获取某个项目正在等待、运行或者运行完毕的job列表。支持的请求类型:GET

  参数:

  • project (string, required) - 项目名称

  request例子:

$ curl http://localhost:6800/listjobs.json?project=myproject

  response例子:

{"status": "ok",
"pending": [{"id": "78391cc0fcaf11e1b0090800272a6d06", "spider": "spider1"}],
"running": [{"id": "422e608f9f28cef127b3d5ef93fe9399", "spider": "spider2", "start_time": "2012-09-12 10:14:03.594664"}],
"finished": [{"id": "2f16646cfcaf11e1b0090800272a6d06", "spider": "spider3", "start_time": "2012-09-12 10:14:03.594664", "end_time": "2012-09-12 10:24:03.594664"}]}

  delversion.json

  删除某个项目的某个版本,如果这个项目不再有能用的版本,那么项目也会被删除。支持的请求方法:POST

  参数:

  • project (string, required) - 项目名称
  • version (string, required) - 要删除的项目版本

  request例子:

$ curl http://localhost:6800/delversion.json -d project=myproject -d version=r99

  

  response例子:  

{"status": "ok"}

  delproject.json

  删除指定项目。支持请求方法:POST

  参数:

  • project (string, required) - 项目名称

  request例子:

$ curl http://localhost:6800/delproject.json -d project=myproject

  response例子:

{"status": "ok"}

  


 

scrapyd 参考(https://www.jianshu.com/p/2a189127901a)的更多相关文章

  1. python cookie登录DVWA,phpstudy搭建DVWA参考https://www.jianshu.com/p/97d874548300

    import requestsheader={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleW ...

  2. [转]https://www.jianshu.com/p/06443248f4d8

    eos是什么? 原文 https://www.jianshu.com/p/06443248f4d8 简介 用一句话来定义eos,即:区块链操作系统,支持在它之上构建dapp,支持智能合约.帐户.身份验 ...

  3. fastdfs(https://www.jianshu.com/p/1c71ae024e5e)

    参考 官方网站:https://github.com/happyfish100/ 配置文档:https://github.com/happyfish100/fastdfs/wiki/ 参考资料:htt ...

  4. 当我写下Map<String,Object> map = new HashMap<>() https://www.jianshu.com/p/6b2e350e99be

    当我写下Map<String,Object> map = new HashMap<>();我到底在写什么? 我什么时候会写HashMap? 一个函数同时需要返回 多种 状态的情 ...

  5. Zabbix调优不完全指南(https://www.jianshu.com/p/2d911d55448f)

    从学习搭建zabbix到完成各类监控.调优.二次开发已经过去了两年,期间通过QQ学习群.zabbix官方社区.各个技术博客整理学习了不少关于各种报错的处理方法,现在将常见的一些报错处理方法整理出来分享 ...

  6. 接口测试之——Charles抓包及常见问题解决(转载自https://www.jianshu.com/p/831c0114179f)

    简介 Charles其实是一款代理服务器,通过成为电脑或者浏览器的代理,然后截取请求和请求结果达到分析抓包的目的.该软件是用Java写的,能够在Windows,Mac,Linux上使用,安装Charl ...

  7. jar与war包区别,转自https://www.jianshu.com/p/3b5c45e8e5bd

    https://www.jianshu.com/p/3b5c45e8e5bd

  8. 参考 https://raspberrypi.stackexchange.com/questions/3617/how-to-install-unrar-nonfree > 1.卸载unrar-free。 $ sudo apt-get remove unrar-free \ 2.通过编辑确保您拥有源存储库/etc/apt/sources.list。 $ cat /etc/apt/sources.

    from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939944 参考 https://raspberrypi.stacke ...

  9. https://www.jianshu.com/p/1038c6170775

    import os # 方法一: os.walk实现 def items_dir(rootname): l = [] for main_dir, dirs, file_name_list in os. ...

随机推荐

  1. Verifying Package Integrity Using MD5 Checksums or GnuPG

    In this note, I reference the MySQL manual file. After downloading the MySQL package that suits your ...

  2. Python原始套接字编程-乾颐堂

    在实验中需要自己构造单独的HTTP数据报文,而使用SOCK_STREAM进行发送数据包,需要进行完整的TCP交互. 因此想使用原始套接字进行编程,直接构造数据包,并在IP层进行发送,即采用SOCK_R ...

  3. [BAT] 通过批处理删除7天前的报告,并删除当前目录下的空文件夹

    set reportPath=D:\AutomationReport cd /d %reportPath% forfiles /p %reportPath% /s /m *.xml /d -7 /c ...

  4. 前端福利之表单input按钮在各浏览器之间的兼容性(转)

    从网上看了这篇关于表单input按钮的浏览器兼容性问题,总结的还不错,所以copy下来学习下. input按钮在各个浏览器之间的兼容性问题,看下边这段代码: input.item { backgrou ...

  5. C# 银行系统

    今天分享一个大家都爱的Money 银行系统代码  可以随心所欲的存钱取钱 //要想成功,必须马到 //建立数组 Card[] cards = ]; //卡类初始化 public void Initia ...

  6. Debug 时,执行语句

    Display View The Display View displays the result of evaluating an expression in the context of the ...

  7. javafx 继承Application打开

    前段时间需要用到javafx的Application来写一些图形界面之类的东西,但是run了之后eclipese不会去运行它,很纳闷,然后看了一下run as发现是没有main入口 其实加上一个mai ...

  8. Java中一对多映射关系

    通过栗子,一个人可以有多辆汽车 定义人   这个类 人可以有很多辆汽车,类中车属性用数组 class Person{ private String name; private String phone ...

  9. mybatis SqlMapConfig.xml

    一.SqlMapConfig.xml 1.属性properties 在入门时,以抽取出连接数据库的属性得到properties文件. a.可以通过resource和url来获得属性. b.proper ...

  10. C#:几种数据库的大数据批量插入 - faib

    在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...