[Database] scrapy redis redis how to operate and use databases

The need to install something into ah shown on FIG.

First, the command line installation redis database, the command line format is as follows:

pip install redis

If the installation is complete redis under the windows, use the command line to test:

安装命令:redis-server.exe --service-install redis.windows.conf --loglevel verbose

启动服务命令:redis-server.exe  --service-start

关闭服务命令:redis-server.exe  --service-stop

Signs and successful installation process is as follows:

About Redis Desktop Manager is installed refer to this URL  https://blog.csdn.net/weixin_42284867/article/details/81219810

First redis database connection:

pool = redis.ConnectionPool(host=‘127.0.0.1‘, port=6379)
r = redis.StrictRedis(connection_pool=pool)

Redis database on this one operation, you can refer https://blog.csdn.net/zhaoliang831214/article/details/82051449

Redis database to obtain data instances of:

redis data to be stored in the form of hash worth redis are detailed below

Get the code below

#其中的db=0可以参考列表左边,redis一共有15个数据库
pool = redis.ConnectionPool(host='localhost',port=6379,password='',db=0)#创建连接池
r = redis.StrictRedis(connection_pool=pool,decode_responses=True)
#第一例 #useful_proxy是一个hash值,于表名
#result =r.hkeys("useful_proxy")
#返回[b'116.196.85.166:3128', b'180.97.33.212:80', b'124.205.155.146:9090'...一系列key值,如上图key那一列
#第二例 获取表中所有的值
result = r.hgetall("useful_proxy")
#返回{b'116.196.85.166:3128': b'{"proxy": "116.196.85.166:3128", "fail_count": 0, "region": ""...后边所有都是类似‘key1’:{“属性1名”:“value”,“属性2名”:“value”},‘key2’:{“属性1名”:“value”,“属性2名”:“value”}...就是hash的结构属性+集合的构造
#第三例 获取表中单个列的value
#result = r.hmget("useful_proxy","116.196.85.166:3128")[0]
#返回{"proxy": "116.196.85.166:3128", "fail_count": 0。。。一个集合类似{“属性1名”:“value”,“属性2名”:“value”}
#第四例
#这里添加.decode()是为了添加value前边那个b’值,把bytes值变为string值
result = r.hmget("useful_proxy","116.196.85.166:3128")[0].decode()
#这里把string变为dict字典型
result = json.loads(result)
result = result.get("proxy")
print(result)

This hash value can be put in the second embodiment, when converted to string, then go dict, to deal with the python language. Haha embarrassment

Published 56 original articles · won praise 2 · views 30000 +

Guess you like

Origin blog.csdn.net/fan13938409755/article/details/105136056