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

相關文章
相關標籤/搜索