首页 > 技术文章 > Redis分布式爬取

dream-czg 2019-04-27 15:59 原文

 

首先我们进行scrapy-redis分布式爬取的时候 先了解一下redis

  1. Redis 是目前公认的速度最快的基于内存的键值对数据库
  2. Redis 作为临时数据的缓存区,可以充分利用内存的高速读写能力大大提高爬虫爬取效率。
  3. scrapy-redis 是为了更方便地实现 Scrapy 分布式爬取,而提供的一些以 Redis 为基础的组件。
  4. scrapy-redis 把 deque 换成 redis 数据库,能让多个 spider 读取同一个 redis 数据库里,解决了分布式的主要问题。

 

那么了解了之后我们进行设置 在项目的 setting当中

# 过滤器  去重
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# 调度器
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 调度状态持久化   也可以不用设置
SCHEDULER_PERSIST = True
# 请求调度使用优先队列   也可以不用设置
SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderPriorityQueue'
# redis 使用的端口和地址  
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
或者使用 REDIS_URL = 'redis://127.0.0.1:6379'
 
 
设置完成之后 我们需要安装 redis可视化的工具 将他打开我们设置 



点击我们进行加入

 

就设置一下名字和host就可以  一般如果需要多台机器进行一起爬取的话那么我们在设置host的时候 在自己电脑cmd命令行内 ipconfig 查看自己的ip地址 写入到host里面,大家还得记得将自己电脑的防火墙关闭 另外一台电脑需要连接你的电脑进行爬取

 

 

将自己代码 py文件内 进行导入RedisCrawlSpider

 

 

我们在安装redis 的文件内找到

 

开启redis服务的图像

 

输入cmd之后出现的命令

 

输入完成之后

 

显示已经完成那么我们在运行我们的项目 就可以了 队列里就有数据了

 

 

遇到的问题 如下:

1. 如果我们开启 redis可视化工具的时候会报下面的错误

 

这是由于我们开启redis服务 将服务开启之后就可以连接了

 

 

 

2. 我们在连接 另外一台电脑进行一起爬取的时候 会请求超时

那么 解决的方法就是 将对方的防火墙进行关闭 这样是连接成功

 

3. 我们在进行远程连接的时候 需要输入外网的ip 10.30. 或者 192.168.等 所以在连接时经常会报错误

链接redis 时只能通过本地localhost (127.0.0.1)这个来链接,而不能用网络ip(192.168..)这个链接,如果用网络ip 链接会报以下的错误:

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

解决的方法:

 

进入它之后我们设置  后面两个不用动也可以

1)打开配置文件把下面对应的注释掉
# bind 127.0.0.1 

2)Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no
daemonize no

3)保护模式
protected-mode no 

 

将它给 注释了 我给大家发的可视化的工具包内的设置已经改完大家就不用动了

推荐阅读