scrapy爬虫在服务器上的部署

  • 部署时需要使用到的模块

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

pip install scrapyd

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

pip install scrapyd-client

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

scrapyd-deploy -h

《scrapy爬虫在服务器上的部署》

修改scrapy项目目录下的scrapy.cfg配置文件

《scrapy爬虫在服务器上的部署》

《scrapy爬虫在服务器上的部署》

  • 改为服务器的公网ip
[deploy]

url=http://localhost:6800

project=项目名称

把代码中的这些修改完成后,即可开始部署工作了

本地部署

项目部署相关命令: 注意这里是项目的名称而不是工程的名称

  • scrapyd-deploy -p <项目名称>

也可以指定版本号

  • scrapyd-deploy -p <项目名称> –version <版本号>

运行爬虫

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

  • win系统curl需要下载点击去下载, 然后将解压目录下的bin目录加入path环境变量中即可使用.

关闭爬虫

curl http://localhost:6800/cancel.json -d project=myproject -d job=’jobid’

获取部署的爬虫项目列表

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

获取项目下的爬虫文件列表

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

获取工程下的爬虫运行状态

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

删除部署的爬虫项目

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

远端部署

第一步,有自己的服务器

《scrapy爬虫在服务器上的部署》

第二步,进入登录

《scrapy爬虫在服务器上的部署》

第三步,进入服务器webShell

《scrapy爬虫在服务器上的部署》 image.png

接下来,配置项目运行环境

  • 配置python环境(ubuntu自带python3环境))
  • 安装pip3:sudo apt install python3-pip
  • 安装scrapy:pip3 install scrapy

如果安装失败添加如下依赖:

  • sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
  • 安装scrapyd: pip3 install scrapyd
  • 安装scrapyd-client: pip3 install scrapyd-client

添加爬虫运行的三方库:

  • pip3 install requests
  • pip3 install pymysql
  • pip3 install pymongodb
    …………..

第五步,修改scrapyd的配置文件,允许外网访问

  • 查找配置文件的路径:find -name default_scrapyd.conf
  • 修改配置文件: sudo vim 路径

    《scrapy爬虫在服务器上的部署》 image.png

    其中有个bind_address 改为0.0.0.0

第六步,去服务器安全组配置

《scrapy爬虫在服务器上的部署》

添加成功后,点击修改规则,添加如下信息(配置目的:允许访问6800端口)

《scrapy爬虫在服务器上的部署》

完成后返回到云主机菜单,找到配置安全组菜单,跟换为刚才添加的安全组

《scrapy爬虫在服务器上的部署》

最终完成后,在浏览器中输入ip地址和端口,显示如下图,说明配置成功

《scrapy爬虫在服务器上的部署》

如果涉及到数据库,则需要在远程服务器中安装mysql

sudo apt update
sudo apt-get install mysql-server mysql-client

  • 紧接着根据提示设置数据数据库密码
  • 找到mysql配置文件并做如下修改:允许远程连接

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

将
bind-address= 127.0.0.1
注释掉或则修改为
bind-address= 0.0.0.0
  • 授权root账户允许远程访问:

grant all privileges on . to root@’%’ identified by ‘password’ with grant option;

    原文作者:凌屿
    原文地址: https://www.jianshu.com/p/305122f5b10b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞