如何免费创建云端爬虫集群
scrapydweb.herokuapp.com
网络拓扑图注册帐号Heroku访问 heroku.com 注册免费账号(注册页面需要调用 google recaptcha 人机验证,登录页面也需要科学地进行上网,访问 app 运行页面则没有该问题),免费账号最多可以创建和运行5个 app。
Redis Labs(可选)访问 redislabs.com 注册免费账号,提供30MB 存储空间,用于下文通过 scrapy-redis 实现分布式爬虫。
通过浏览器部署 Heroku app访问 my8100/scrapyd-cluster-on-heroku-scrapyd-app 一键部署 Scrapyd app。(注意更新页面表单中 Redis 服务器的主机,端口和密码)重复第1步完成4个 Scrapyd app 的部署,假设应用名称为svr-1
, svr-2
, svr-3
和 svr-4
访问 my8100/scrapyd-cluster-on-heroku-scrapydweb-app 一键部署 ScrapydWeb app,取名 myscrapydweb
(可选)点击 dashboard.heroku.com/apps/myscrapydweb/settings 页面中的 Reveal Config Vars 按钮相应添加更多 Scrapyd server,例如 KEY 为 SCRAPYD_SERVER_2
, VALUE 为 svr-2.herokuapp.com:80#group2
访问 myscrapydweb.herokuapp.com跳转 部署和运行分布式爬虫 章节继续阅读。自定义部署安装工具GitHeroku CLIPython client for Redis:运行 pip install redis
命令即可。下载配置文件新开一个命令行提示符:
git clone https://github.com/my8100/scrapyd-cluster-on-herokucd 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 集群
新建 Git 仓库
cd scrapydgit init# explore and update the files if neededgit statusgit add .git commit -a -m "first commit"git status
部署 Scrapyd app
heroku apps:create svr-1heroku git:remote -a svr-1git remote -vgit push heroku masterheroku logs --tail# Press ctrl+c to stop logs outputting# Visit https://svr-1.herokuapp.com
添加环境变量
设置时区# 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-hostheroku config:set REDIS_PORT=your-redis-portheroku config:set REDIS_PASSWORD=your-redis-password
重复上述第2步和第3步完成余下三个 Scrapyd app 的部署和配置:svr-2
,svr-3
和 svr-4
创建 ScrapydWeb app
新建 Git 仓库
cd ..cd scrapydwebgit init# explore and update the files if neededgit statusgit add .git commit -a -m "first commit"git status
部署 ScrapydWeb app
heroku apps:create myscrapydwebheroku git:remote -a myscrapydwebgit remote -vgit push heroku master
添加环境变量
设置时区heroku config:set TZ=Asia/Shanghai
添加 Scrapyd server(详见 scrapydweb 目录下的 scrapydweb_settings_v8.py)
heroku config:set SCRAPYD_SERVER_1=svr-1.herokuapp.com:80heroku config:set SCRAPYD_SERVER_2=svr-2.herokuapp.com:80#group1heroku config:set SCRAPYD_SERVER_3=svr-3.herokuapp.com:80#group1heroku config:set SCRAPYD_SERVER_4=svr-4.herokuapp.com:80#group2
访问 myscrapydweb.herokuapp.com
部署和运行分布式爬虫上传 demo 项目,即 scrapyd-cluster-on-heroku 目录下的压缩文档 scrapy_redis_demo_project.zip将种子请求推入 mycrawler:start_urls
触发爬虫并查看结果
In [1]: import redis # pip install redisIn [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]: 0In [4]: r.lpush('mycrawler:start_urls', 'http://books.toscrape.com', 'http://quotes.toscrape.com')Out[4]: 2# wait for a minuteIn [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"}']
my8100/scrapyd-cluster-on-heroku
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。