scrapy爬虫部署

第一步:安装使用到的相关库,终端进入有XXX.cfg文件的这个目录下

scrapyd

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

pip3 install scrapyd

scrapyd-client

  • 发布爬虫需要使用另一个专用工具,就是将代码打包为EGG文件,其次需要将EGG文件上传到远程主机上这些操作需要scrapyd-client来帮助我们完成

pip3 install scrapyd-client

  • 安装完成后可以使用如下命令来检查是否安装成功

scrapyd-deploy -h

出现以下信息表示下载成功
Usage: scrapyd-deploy [options] [ [target] | -l | -L  ]

Deploy Scrapy project to Scrapyd server

Options:
  -h, --help            show this help message and exit
  -p PROJECT, --project=PROJECT
                        the project name in the target
  -v VERSION, --version=VERSION
                        the version to deploy. Defaults to current timestamp
  -l, --list-targets    list available targets
  -a, --deploy-all-targets
                        deploy all targets
  -d, --debug           debug mode (do not remove build dir)
  -L TARGET, --list-projects=TARGET
                        list available projects on TARGET
  --egg=FILE            use the given egg, instead of building it
  --build-egg=FILE      only build the egg, don't deploy it

注意:如果你使用windows的系统,需要进行以下配置

  1. 注意你的python运行环境,如果你是虚拟环境下运行的,你需要找到你的虚拟环境目录下的Scripts文件夹。新建一个:scrapyd-deploy.bat文件,如下图:


    image.png
  2. 打开这个文件写入:
@echo off
"D:\python3.5\python3.exe(你当前环境的python解释器路径)" "D:\python3.5\Scripts\scrapyd-deploy(存放scrapyd-deploy文件的路径)" %1 %2 %3 %4 %5 %6 %7 %8 %9
  • 输入命令

scrapyd-deploy -h
出现以上那一堆信息说明安装成功

第二步:修改scrapy项目目录下的scrapy.cfg配置文件

  • 首先需要修改scrapy.cfg (项目的配置文件)
[deploy]
url=http://localhost:6800 # 本地
# 部署云服务器
url = 云服务器公网IP
project=项目名称

第三步:运行环境

scrapyd

常用命令

  • 添加项目

scrapyd-deploy -p 项目名称 --version 1.0(version版本号,可以省略)

  • 安装curl
    ubuntu:sudo apt-get install curl
    windows: curl官网下载
  • 调度爬虫开始运行

curl http://localhost:6800/schedule.json -d project=项目名称 -d spider=xcfCrawlSpider

  • 关闭爬虫

curl http://localhost:6800/cancel.json -d project=项目名称 -d job="jobid"

  • 获取部署的项目列表

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

  • 获取项目的版本号

curl http://localhost:6800/listversions.json?project=项目名称

  • 获取项目下的爬虫文件

curl http://localhost:6800/listspiders.json?project=项目名称

  • 获取爬虫的运行状态

curl http://localhost:6800/listjobs.json?project=项目名称

  • 删除对应版本的项目

curl http://localhost:6800/delversion.json -d project=项目名称 -d version=r99

  • 直接删除项目

curl http://localhost:6800/delproject.json -d project=项目名称

你可能感兴趣的:(scrapy爬虫部署)