1.spider 修改
class CgysSpider(scrapy.Spider):
name = 'clispider'
start_urls = ['https://search.bilibili.com/all?keyword=%E6%A9%99%E6%9E%9C%E5%8C%BB%E7%94%9F']
# 改成
from scrapy_redis.spiders import RedisSpider
class ClispiderSpider(RedisSpider):
name = 'clispider'
redis_key = 'cl5:start_urls'
2.setting.py修改
# 1(必须). 使用了scrapy_redis的去重组件,在redis数据库里做去重
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# 2(必须). 使用了scrapy_redis的调度器,在redis里分配请求
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 3(必须). 在redis中保持scrapy-redis用到的各个队列,从而允许暂停和暂停后恢复,也就是不清理redis queues
SCHEDULER_PERSIST = True
# 4(必须). 通过配置RedisPipeline将item写入key为 spider.name : items 的redis的list中,供后面的分布式处理item
# 这个已经由 scrapy-redis 实现,不需要我们写代码,直接使用即可
ITEM_PIPELINES = {
# 'AQI.pipelines.AqiJsonPipeline': 200,
# 'AQI.pipelines.AqiCSVPipeline': 300,
# 'AQI.pipelines.AqiRedisPipeline': 400,
# 'AQI.pipelines.AqiMongoPipeline': 500,
'scrapy_redis.pipelines.RedisPipeline': 100
}
# 5(必须). 指定redis数据库的连接参数
REDIS_HOST = "192.168.1.235"
REDIS_PORT = 6379
REDIS_PARAMS = {
"password": "KangCe@0608",
}
# 6.如果不启用则按scrapy默认的策略
# -1. 默认的 按优先级排序(Scrapy默认),由sorted set实现的一种非FIFO、LIFO方式。
# SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderPriorityQueue'
# -2. 可选的 按先进先出排序(FIFO)
# SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderQueue'
# -3. 可选的 按后进先出排序(LIFO)
# SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderStack'
# 7. LOG等级
#LOG_LEVEL = 'DEBUG'
3.启动scrapy-reids
scarpy crawl clispider
或者启动多个
scrapy crawlall
4.上传url到reids
lpush sinaspider:start_urls http://news.sina.com.cn/guide/
或
from redis import ConnectionPool, StrictRedis
import os
REDIS_HOST = "115.238.111.198"
REDIS_PORT = "6380"
REDIS_PASSWORK = "KangCe@0608"
pool = ConnectionPool(host=REDIS_HOST,
port=REDIS_PORT,
db=0,
password=REDIS_PASSWORK,
)
# password=REDIS_PASSWD)
conn = StrictRedis(connection_pool=pool)
# password=REDIS_PASSWD)
# juzi_key,coal_dict,key4
path = 'D:\\02-python\\04-work\\80-bt\\clili\\key\\aaa'
name_list = os.listdir(path)
for name in name_list:
with open("%s\\%s" % (path, name), encoding='utf-8') as f:
keys = set(f.readlines())
for _key in keys:
key = _key.strip()
conn.lpush("juzi:start_urls",
"http://juzicili.com/btlist/{}.html ".format(key)) # juzikey\coal_4\coal_3\coal_2\coal_1\figu_1\figu_0\key1
conn.lpush("diaosisou:start_urls",
"http://www.diaosisou.cc/list/{}".format(key)) # juzikey\coal_4\coal_3\coal_2\figu_1\figu_0\key1
conn.lpush("cl5:start_urls",
"https://www.cl5.org/kw/{}.html".format(key)) # juzikey\coal_4\coal_3\coal_2\coal_1\coal_0\figu_1\figu_0\key1
conn.lpush("sosobtx:start_urls", "https://www.sosobtx.com/s/{}.html".format(key))# juzikey\coal_4\key1
conn.lpush("btrabbit:start_urls",
"https://www.btrabbit.la/search/{}".format(key)) # juzikey\coal_4\coal_3\coal_2\figu_1\figu_0\key1
print("{}:push success".format(key))
5.常用的reids命令
keys * 查看所以建
llen key 查看列表长度
redis
redis-cli -h 192.168.1.235 -p 6379 -auth KangCe@0608
ltrim [ltrim key range_l range_r]:保留区域类的元素,其他的删除
ltrim plist 0 3
lpop 、rpop:分别为删除头部和尾部第一个元素,返回被删除的元素
lpop plist
rpop plist