如何免費建立雲端爬蟲集羣

在線體驗

scrapydweb.herokuapp.comhtml

註冊賬號

  1. Heroku

訪問 heroku.com 註冊免費帳號(註冊頁面須要調用 google recaptcha 人機驗證,登陸頁面也須要科學地進行上網,訪問 app 運行頁面則沒有該問題),免費帳號最多能夠建立和運行5個 apppython

heroku register

  1. Redis Labs(可選)

訪問 redislabs.com 註冊免費帳號,提供30MB 存儲空間,用於下文經過 scrapy-redis 實現分佈式爬蟲git

redislabs register

經過瀏覽器部署 Heroku app

  1. 訪問 my8100/scrapyd-cluster-on-heroku-scrapyd-app 一鍵部署 Scrapyd app。(注意更新頁面表單中 Redis 服務器的主機,端口和密碼)
  2. 重複第1步完成4個 Scrapyd app 的部署,假設應用名稱爲 svr-1, svr-2, svr-3svr-4
  3. 訪問 my8100/scrapyd-cluster-on-heroku-scrapydweb-app 一鍵部署 ScrapydWeb app,取名 myscrapydweb
  4. (可選)點擊 dashboard.heroku.com/apps/myscra… 頁面中的 Reveal Config Vars 按鈕相應添加更多 Scrapyd server,例如 KEY 爲 SCRAPYD_SERVER_2, VALUE 爲 svr-2.herokuapp.com:80#group2
  5. 訪問 myscrapydweb.herokuapp.com
  6. 跳轉 部署和運行分佈式爬蟲 章節繼續閱讀。

自定義部署

查看內容

安裝工具

  1. Git
  2. Heroku CLI
  3. Python client for Redis:運行 pip install redis 命令便可。

下載配置文件

新開一個命令行提示符:github

git clone https://github.com/my8100/scrapyd-cluster-on-heroku
cd scrapyd-cluster-on-heroku
複製代碼

登陸 Heroku

heroku login
# outputs:
# heroku: Press any key to open up the browser to login or q to exit:
# Opening browser to https://cli-auth.heroku.com/auth/browser/12345-abcde
# Logging in... done
# Logged in as username@gmail.com
複製代碼

建立 Scrapyd 集羣

  1. 新建 Git 倉庫
cd scrapyd
git init
# explore and update the files if needed
git status
git add .
git commit -a -m "first commit"
git status
複製代碼
  1. 部署 Scrapyd app
heroku apps:create svr-1
heroku git:remote -a svr-1
git remote -v
git push heroku master
heroku logs --tail
# Press ctrl+c to stop logs outputting
# Visit https://svr-1.herokuapp.com
複製代碼
  1. 添加環境變量web

    • 設置時區
    # python -c "import tzlocal; print(tzlocal.get_localzone())"
    heroku config:set TZ=Asia/Shanghai
    # heroku config:get TZ
    複製代碼
    • 添加 Redis 帳號(可選,詳見 scrapy_redis_demo_project.zip 中的 settings.py
    heroku config:set REDIS_HOST=your-redis-host
    heroku config:set REDIS_PORT=your-redis-port
    heroku config:set REDIS_PASSWORD=your-redis-password
    複製代碼
  2. 重複上述第2步和第3步完成餘下三個 Scrapyd app 的部署和配置:svr-2svr-3svr-4redis

建立 ScrapydWeb app

  1. 新建 Git 倉庫
cd ..
cd scrapydweb
git init
# explore and update the files if needed
git status
git add .
git commit -a -m "first commit"
git status
複製代碼
  1. 部署 ScrapydWeb app
heroku apps:create myscrapydweb
heroku git:remote -a myscrapydweb
git remote -v
git push heroku master
複製代碼
  1. 添加環境變量數據庫

    • 設置時區
    heroku config:set TZ=Asia/Shanghai
    複製代碼
    • 添加 Scrapyd server(詳見 scrapydweb 目錄下的 scrapydweb_settings_v8.py
    heroku config:set SCRAPYD_SERVER_1=svr-1.herokuapp.com:80
    heroku config:set SCRAPYD_SERVER_2=svr-2.herokuapp.com:80#group1
    heroku config:set SCRAPYD_SERVER_3=svr-3.herokuapp.com:80#group1
    heroku config:set SCRAPYD_SERVER_4=svr-4.herokuapp.com:80#group2
    複製代碼
  2. 訪問 myscrapydweb.herokuapp.com 瀏覽器

    scrapydweb

部署和運行分佈式爬蟲

  1. 上傳 demo 項目,即 scrapyd-cluster-on-heroku 目錄下的壓縮文檔 scrapy_redis_demo_project.zip
  2. 將種子請求推入 mycrawler:start_urls 觸發爬蟲並查看結果
In [1]: import redis  # pip install redis

In [2]: r = redis.Redis(host='your-redis-host', port=your-redis-port, password='your-redis-password')

In [3]: r.delete('mycrawler_redis:requests', 'mycrawler_redis:dupefilter', 'mycrawler_redis:items')
Out[3]: 0

In [4]: r.lpush('mycrawler:start_urls', 'http://books.toscrape.com', 'http://quotes.toscrape.com')
Out[4]: 2

# wait for a minute
In [5]: r.lrange('mycrawler_redis:items', 0, 1)
Out[5]:
[b'{"url": "http://quotes.toscrape.com/", "title": "Quotes to Scrape", "hostname": "d6cf94d5-324e-4def-a1ab-e7ee2aaca45a", "crawled": "2019-04-02 03:42:37", "spider": "mycrawler_redis"}',
 b'{"url": "http://books.toscrape.com/index.html", "title": "All products | Books to Scrape - Sandbox", "hostname": "d6cf94d5-324e-4def-a1ab-e7ee2aaca45a", "crawled": "2019-04-02 03:42:37", "spider": "mycrawler_redis"}']
複製代碼

scrapyd cluster on heroku

總結

  • 優勢
    • 免費
    • 能夠爬 Google 等外網
    • 可擴展(藉助於 ScrapydWeb
  • 缺點
    • 註冊和登陸須要科學地進行上網
    • Heroku app 天天至少自動重啓一次而且重置全部文件,所以須要外接數據庫保存數據,詳見 devcenter.heroku.com

GitHub 開源

my8100/scrapyd-cluster-on-herokubash

相關文章
相關標籤/搜索