分布式爬蟲實戰


一、環境搭建

由於條件有限,一台虛擬機,一台筆記本。

  • 在虛擬機上裝上mongodb數據庫、redis數據庫、redis_scrapy、pymongo、scrapyd
  • 在本地電腦上裝上monodb數據庫、redis數據庫、redis_scrapy、pymongo、scrapyd-clientl、scrapyd-api

二、修改爬蟲項目的settings

如果使用分布式爬蟲,配置文件需要做以下修改:

# -*- coding: utf-8 -*-

# Scrapy settings for zhihu project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'zhihu'

SPIDER_MODULES = ['zhihu.spiders']
NEWSPIDER_MODULE = 'zhihu.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'zhihu (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
':authority': 'www.zhihu.com'
}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'zhihu.middlewares.ZhihuSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'zhihu.middlewares.ZhihuDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   # 'zhihu.pipelines.ZhihuPipeline': 300,
   'zhihu.pipelines.MongoPipeline': 300,
   'scrapy_redis.pipelines.RedisPipeline': 301,

}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

MONGO_URI="localhost"
MONGO_DATABASE="zhihu"


SCHEDULER = "scrapy_redis.scheduler.Scheduler"

DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

# SCHEDULER_PERSIST=True #爬取的requests以及dupefilter會保存在數據庫中

REDIS_URL = 'redis://root:bright@192.168.0.104:6379'
# REDIS_URL = 'redis:127.0.0.1:6379'

在以前的配置文件中加入:

 'scrapy_redis.pipelines.RedisPipeline': 301, #pipeline配置中加入
   
SCHEDULER = "scrapy_redis.scheduler.Scheduler"

  DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

這樣完成了增量式的爬蟲,但還不是分布式的爬蟲。

三、scrapyd部署爬蟲項目

1、在爬蟲項目的scrapy.cfg中進行如下修改

# Automatically created by: scrapy startproject
#
# For more information about the [deploy] section see:
# https://scrapyd.readthedocs.io/en/latest/deploy.html

[settings]
default = zhihu.settings

[deploy]
url = http://192.168.0.104:6800/addversion.json #表示要上傳主機的ip
project = zhihu

詳細配置可參考:https://scrapyd.readthedocs.io/en/latest/api.html

2、在linux主機上先啟動scrapyd服務

3、上傳項目

這樣可以上傳項目到服務上,並且訪問網址查看當前的狀態

 

目前沒有上傳任何項目,此時進行上傳,筆者是在pycharm中直接上傳的

此處可能有坑:

(1)報錯一

  'scrapyd-deploy' 不是內部或外部命令,也不是可運行的程序 或批處理文件

  這時可以需要在你當前python環境的目錄下建立scrapyd-deploy.bat文件,里面填充,注意是自己的python路徑以及scrapyd-deploy路徑,因為這是在windows下執行命令。

"C:\Users\Administrator\Envs\automatic\Scripts\python.exe"  "C:\Users\Administrator\Envs\automatic\Scripts\scrapyd-deploy"  %*

(2)報錯二

   builtins.AttributeError: 'int' object has no attribute 'splitlines'

  這是因為版本不匹配的原因,將linux服務器上的相應包進行回退

  Scrapy==1.6.0 Twisted==18.9.0

    參考文章:https://blog.csdn.net/qq_29719097/article/details/89431234 

當一切順利后,可以查看是否上傳成功

已經上傳成功,那么在linux上如何開啟爬蟲呢?

scrapyd中有很多接口,包括如何工作之類的接口,但都是以url來進行調度的,在這里可以使用scrapyd-api,這是將scrapyd進行封裝,以方法的方式進行調度,比較簡單,進行相應的python環境中就可以使用了。

(4)啟動linux上的爬蟲項目

在啟動前先確保linux上的redis,以及mongodb服務已經開啟,因為配置文件中進行了配置,在這里利用scrapy-api進行開啟任務。詳細使用查看https://python-scrapyd-api.readthedocs.io/en/latest/usage.html#calling-the-api

可以在web上查看目前狀態

(5)查看數據庫

可以查看redis數據庫,里面包含請求requests、item、dupefilter數據

另外,mongodb中的數據也進行了分布式的爬取,不同主機爬取的放入不同主機下的mongodb數據庫中

這樣就完成了分布式的爬取。

四、總結

(1)一系列包的安裝

(2)linux服務器上開啟scrapyd、mongodb、redis服務

(3)本地部署上傳(更改scrapy.cfg文件,scrapyd-deploy命令上傳(需要安裝scrapyd-client))

(4)利用scrapyd-api在python環境中進行開啟爬蟲

(5)本地同時利用scrapy crawl spider命令進行爬取

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM