美文网首页程序员
Scrapyd项目部署

Scrapyd项目部署

作者: 8f3a71b379c1 | 来源:发表于2018-07-04 16:49 被阅读296次

    一、简介

    scrapyd是运行scrapy爬虫的服务程序,它支持以http命令方式发布、删除、启动、停止爬虫程序。而且scrapyd可以同时管理多个爬虫,每个爬虫还可以有多个版本。

    特点:
    1、可以避免爬虫源码被看到。
    2、有版本控制。
    3、可以远程启动、停止、删除

    二、安装

    pip install scrapyd

    有个必备的组件库:
    scrapy 通过pip安装 1.5.0
    enum-compat ,通过pip 安装 0.0.2
    scrapyd-client 通过pip 安装 1.1.0
    w3lib 通过pip 安装 1.19.0

    三、使用

    1. 启动scrapyd
      在终端中,运行 scrapyd


      会显示如上信息,scrapyd默认绑定 6800 ,可以通过 http://127.0.0.1:6800 访问,访问后显示页面如下:
    2. 发布项目
      修改Scrapy项目scrapy.cfg配置文件


      修改前
      修改后
    • 首先去掉url前面的注释符号,url是scrapyd服务器的网址
    • 然后project=tenCent为项目名称,可以随意起名
    • 修改[deploy]为[deploy:100],表示把爬虫发布到名为100的爬虫服务器上,一般在需要同时发布爬虫到多个目标服务器时使用
    1. 发布爬虫

    命令如下:

    scrapyd-deploy <target> -p <project> --version <version>

    参数解释:

    • target:deploy后面的名称
    • project:自行定义名称,跟爬虫的工程名字无关
    • version:自定义版本号,不写的话默认为当前时间戳

    上传项目成功

    4. 控制API

    所有的API都是通过http协议发送的请求,目前总共10个api

    规则是:http://ip:port/api_command.json,有GET和POST两种请求

    1. 检查服务的状态 (daemonstatus.json)
      GET请求:

    curl http://localhost:6800/daemonstatus.json
    结果示例:
    {"node_name": "ubuntu", "status": "ok", "finished": 0, "pending": 0, "running": 0}

    1. 增加项目到服务器,如果项目已经存在,则增加一个新的版本 (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
    结果示例:
    {"status": "ok", "spiders": 3}

    1. 启动一个爬虫项目 (schedule.json)
      POST请求:
    • project (string, required) – 项目名

    • spider (string, required) – 爬虫名,spider类中指定的name

    • setting (string, optional) – 自定义爬虫settings

    • jobid (string, optional) – jobid,之前启动过的spider,会有一个id

    • _version (string, optional) – 版本号,之前部署的时候的version,只能使用int数据类型,没指定,默认启动最新版本

    • 其他额外的参数都会放入到spider的参数中

    curl http://localhost:6800/schedule.json -d project=myproject -d spider=somespider
    结果示例:

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

    1. 取消一个 spdier 的运行 (cancel.json)
      如果 spider 是运行状态,则停止其运行
      如果 spider 是 挂起状态,则删除spider
      POST请求:
    • project (string, required) – 项目名
    • job (string, required) -jobid

    curl http://localhost:6800/cancel.json -d project=myproject -d job=6487ec79947edab326d6db28a2d86511e8247444
    结果示例:
    {"status": "ok", "prevstate": "running"}

    1. 获取当前已上传的项目的列表 (listprojects.json)
      GET请求:

    curl http://localhost:6800/listprojects.json
    结果示例:
    {"status": "ok", "projects": ["myproject", "otherproject"]}

    1. 获取指定项目的可用版本 (listversions.json)
      GET请求:
    • project (string, required) – 项目名

    curl http://localhost:6800/listversions.json?project=myproject
    结果示例:
    {"status": "ok", "versions": ["r99", "r156"]}

    1. 获取指定版本的项目中的爬虫列表,如果没有指定版本,则是最新版本 (listspiders.json)
      GET请求:
    • project (string, required) – 项目名
    • _version (string, optional) – 版本号

    curl http://localhost:6800/listspiders.json?project=myproject
    结果示例:
    {"status": "ok", "spiders": ["spider1", "spider2", "spider3"]}

    1. 获取指定项目中 所有挂起、运行和运行结束的job (listjobs.json)
      GET请求
    • project (string, option) - restrict results to project name

    curl http://localhost:6800/listjobs.json?project=myproject | python -m json.tool
    结果示例:
    {
    "status": "ok",
    "pending": [
    {
    "project": "myproject", "spider": "spider1",
    "id": "78391cc0fcaf11e1b0090800272a6d06"
    }
    ],
    "running": [
    {
    "id": "422e608f9f28cef127b3d5ef93fe9399",
    "project": "myproject", "spider": "spider2",
    "start_time": "2012-09-12 10:14:03.594664"
    }
    ],
    "finished": [
    {
    "id": "2f16646cfcaf11e1b0090800272a6d06",
    "project": "myproject", "spider": "spider3",
    "start_time": "2012-09-12 10:14:03.594664",
    "end_time": "2012-09-12 10:24:03.594664"
    }
    ]
    }

    1. 删除指定项目的指定版本 (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
    结果示例:
    {"status": "ok"}

    10 .删除指定项目,并且包括所有的版本 (delproject.json)
    POST请求

    • project (string, required) - the project name

    curl http://localhost:6800/delproject.json -d project=myproject
    结果示例:
    {"status": "ok"}

    相关文章

      网友评论

        本文标题:Scrapyd项目部署

        本文链接:https://www.haomeiwen.com/subject/crbeuftx.html