爬虫(26)scrapy_redis讲解

本文介绍了如何使用scrapy_redis实现Python爬虫的分布式策略。首先讲解了Python与Redis的交互,包括安装redis、连接Redis以及数据操作。接着,阐述了集群与分布式概念,并对比了scrapy与scrapy_redis的区别,强调scrapy_redis支持分布式爬虫。最后,指导读者下载并了解scrapy_redis的案例项目,包括项目结构和关键文件。
摘要由CSDN通过智能技术生成

第二十三章 scrapy_redis讲解

1. python和redis的交互

首先安装redis,pip install redis。

Collecting redis
  Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
     |████████████████████████████████| 72 kB 207 kB/s
Installing collected packages: redis
Successfully installed redis-3.5.3


在pycharm里新建一个py文件redis_crawl.py
然后是连接redis,需要连接的地址和端口号
我们可以定义一个类,在初始化方法里面定义连接方法。

import redis  # 首先导入redis模块

class StringRedis():
    def __init__(self):
        # 连接redis
        self.r = redis.StrictRedis(host='127.0.0.1',port=6379)
        # 定义set方法
    def string_set(self,k,v):
        res = self.r.set(k,v)
        print(res)


if __name__ == '__main__':
    s = StringRedis()
    s.string_set('name','Jerry777')


我们运行一下:

    conn = self.connection or pool.get_connection(command_name, **options)
  File "D:\Python38\lib\site-packages\redis\connection.py", line 1192, in get_connection
    connection.connect()
  File "D:\Python38\lib\site-packages\redis\connection.py", line 563, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. 由于目标计算机积极拒绝,无法连接。.


结果报了个错,我刚才把Redis服务关闭了,这里需要重新打开:


D:\Download\redis-latest>cd redis-latest

D:\Download\redis-latest\redis-latest>redis-server.exe
[14424] 11 Mar 14:47:45.200 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server.exe /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.503 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 14424
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              	
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值