部署Scrapy分布式爬虫项目

部署Scrapy分布式爬虫项目

一、需求分析

初级用户:

  • 只有一台开发主机
  • 能够通过 Scrapyd-client 打包和部署 Scrapy 爬虫项目,以及通过 Scrapyd JSON API 来控制爬虫,感觉命令行操作太麻烦,希望能够通过浏览器直接部署和运行项目

专业用户:

  • 有 N 台云主机,通过 Scrapy-Redis 构建分布式爬虫
  • 希望集成身份认证
  • 希望在页面上直观地查看所有云主机的运行状态
  • 希望能够自由选择部分云主机,批量部署和运行爬虫项目,实现集群管理
  • 希望自动执行日志分析,以及爬虫进度可视化
  • 希望在出现特定类型的异常日志时能够及时通知用户,包括自动停止当前爬虫任务

二、动图预览

集群多节点部署项目和运行爬虫:

部署Scrapy分布式爬虫项目

三、安装和配置

1、请先确保所有主机都已经安装和启动 Scrapyd,如果需要远程访问 Scrapyd,则需将 Scrapyd 配置文件中的 bind_address 修改为 bind_address = 0.0.0.0,然后重启 Scrapyd。

2、开发主机或任一台主机安装 ScrapydWebpip install scrapydweb

3、运行命令 scrapydweb -h将在当前工作目录生成配置文件 scrapydweb_settings.py,可用于下文的自定义配置。

4、启用 HTTP 基本认证:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ENABLE_AUTH = True
USERNAME = 'username'
PASSWORD = 'password'</pre>

5、添加 Scrapyd server,支持字符串和元组两种配置格式,支持添加认证信息和分组/标签:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SCRAPYD_SERVERS = [
'127.0.0.1',
# 'username:password@localhost:6801#group',
('username', 'password', 'localhost', '6801', 'group'),
]</pre>

6、通过运行命令 scrapydweb 启动 ScrapydWeb

四、访问 Web UI

通过浏览器访问并登录 http://127.0.0.1:5000

  • Overview 页面自动输出所有 Scrapyd server 的运行状态
  • 通过分组和过滤可以自由选择若干台 Scrapyd server,调用 Scrapyd 提供的所有 HTTP JSON API,实现一次操作,批量执行

部署Scrapy分布式爬虫项目

五、部署项目

  • 支持指定若干台 Scrapyd server 部署项目
  • 通过配置 SCRAPY_PROJECTS_DIR 指定 Scrapy 项目开发目录,ScrapydWeb 将自动列出该路径下的所有项目,选择项目后即可自动打包和部署指定项目:

部署Scrapy分布式爬虫项目

  • 如果 ScrapydWeb 运行在远程服务器上,除了通过当前开发主机上传常规的 egg 文件,也可以将整个项目文件夹添加到 zip/tar/tar.gz 压缩文件后直接上传即可,无需手动打包:

部署Scrapy分布式爬虫项目

六、运行爬虫

  • 通过下拉框直接选择 project,version 和 spider
  • 支持传入 Scrapy settings 和 spider arguments
  • 同样支持指定若干台 Scrapyd server 运行爬虫

部署Scrapy分布式爬虫项目

七、日志分析和可视化

  • 默认情况下,ScrapydWeb 将在后台定时自动读取和分析 Scrapy log 文件并生成 Stats 页面

部署Scrapy分布式爬虫项目

  • 爬虫进度可视化

部署Scrapy分布式爬虫项目

八、邮件通知

基于后台定时读取和分析 Scrapy log 文件,ScrapydWeb 将在满足特定触发器时发送通知邮件,邮件正文包含当前运行任务的统计信息。

1、添加邮箱帐号:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n85" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SMTP_SERVER = 'smtp.qq.com'
SMTP_PORT = 465
SMTP_OVER_SSL = True
SMTP_CONNECTION_TIMEOUT = 10

FROM_ADDR = 'username@qq.com'
EMAIL_PASSWORD = 'password'
TO_ADDRS = ['username@qq.com']</pre>

2、设置邮件工作时间和基本触发器,以下示例代表:每隔1小时或某一任务完成时,并且当前时间是工作日的9点,12点和17点,ScrapydWeb 将会发送通知邮件。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n87" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">EMAIL_WORKING_DAYS = [1, 2, 3, 4, 5]
EMAIL_WORKING_HOURS = [9, 12, 17]
ON_JOB_RUNNING_INTERVAL = 3600
ON_JOB_FINISHED = True</pre>

3、除了基本触发器,ScrapydWeb 还提供了多种触发器用于处理不同类型的 log,包括 'CRITICAL', 'ERROR', 'WARNING', 'REDIRECT', 'RETRY' 和 'IGNORE'等。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">LOG_CRITICAL_THRESHOLD = 3
LOG_CRITICAL_TRIGGER_STOP = True
LOG_CRITICAL_TRIGGER_FORCESTOP = False

...

LOG_IGNORE_TRIGGER_FORCESTOP = False</pre>

以上示例代表:当发现3条或3条以上的 critical 级别的 log 时,ScrapydWeb 自动停止当前任务,如果当前时间在邮件工作时间内,则同时发送通知邮件。

九、GitHub 开源

活捉几只官方大佬,赶紧前去围观吧,别忘了 Star 噢!

https://github.com/my8100/scrapydweb

相关推荐