部署Scrapy分布式爬虫项目

1、需求分析

初级用户:git

  • 只有一台开发主机
  • 可以经过 Scrapyd-client 打包和部署 Scrapy 爬虫项目,以及经过 Scrapyd JSON API 来控制爬虫,感受命令行操做太麻烦,但愿可以经过浏览器直接部署和运行项目

专业用户:github

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

2、动图预览

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

加QQ群code923414804/code与我一块儿学习,群里有我学习过程当中整理的大量学习资料。加群便可免费获取

3、安装和配置

一、请先确保全部主机都已经安装和启动 Scrapyd,若是须要远程访问 Scrapyd,则需将 Scrapyd 配置文件中的 bind_address 修改成 bind_address = 0.0.0.0,而后重启 Scrapyd。浏览器

二、开发主机或任一台主机安装 ScrapydWebpip install scrapydweb服务器

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

四、启用 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>ide

五、添加 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>spa

六、经过运行命令 scrapydweb 启动 ScrapydWeb

4、访问 Web UI

经过浏览器访问并登陆 http://127.0.0.1:5000

  • Overview 页面自动输出全部 Scrapyd server 的运行状态
  • 经过分组和过滤能够自由选择若干台 Scrapyd server,调用 Scrapyd 提供的全部 HTTP JSON API,实现一次操做,批量执行

image

5、部署项目

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

image

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

image

6、运行爬虫

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

image

7、日志分析和可视化

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

image

  • 爬虫进度可视化

image

8、邮件通知

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

一、添加邮箱账号:

<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>

二、设置邮件工做时间和基本触发器,如下示例表明:每隔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>

三、除了基本触发器,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 自动中止当前任务,若是当前时间在邮件工做时间内,则同时发送通知邮件。

9、GitHub 开源

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

https://github.com/my8100/scrapydweb

相关文章
相关标签/搜索