scrapy-redis分布式爬虫的使用总结

scrapy-redis使用总结

因为我是第一次在使用这个分布式爬虫的过程中踩了很多洞,所以我想在这里总结一下这个爬虫的使用。

[En]

Because I stepped on a lot of holes in using this distributed crawler for the first time, I would like to summarize the use of this crawler here.

安装模块

scrapy-redis 安装:

pip install scrapy-redis

scrapy-redis 注意:
因为调用的时候使用的是下面的导包 我一直 pip install RedisCrawlSpider 了半天。竟然在这卡了半天,真丢人:

from scrapy_redis.spiders import RedisCrawlSpider

settings.py配置

scrapy本身不支持分布式,因为他的管道和调度器不能共享,所以需要配置一下。
1.首先需要在自己写的爬虫文件里注释掉
allowed_domains=[‘xxx.com’],
start_urls = [‘xxx.com’]
2.在爬虫文件中 写上自己的 redis_key
例如:redis_key = ‘test’
3.配置settings


DUPEFILER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

SCHEDULER = "scrapy_redis.scheduler.Scheduler"

SCHEDULER_PERSIST = True

REDIS_HOST = '***.**.**.***'
REDIS_PORT = 6379
REDIS_ENCODING = 'utf-8'
REDIS_PARAMS = {'password':'******'}

redis 配置

首先如果是远程连接redis的话需要配置redis的配置文件:
1.首先找到 bind 将原来的127.0.0.1 修改为 0.0.0.0 允许远程连接。

scrapy-redis分布式爬虫的使用总结2.将 protected-model 改为 no
scrapy-redis分布式爬虫的使用总结3.因为是远程连接涉及到安全问题,最好设置redis密码
scrapy-redis分布式爬虫的使用总结4.在redis 中建立队列 也就是之前的 redis_key
lpush test https://www.baidu.com/

切换到 spiders 目录下
运行scrapy runspider 爬虫文件.py

总结

因为这是第一次使用这种分布式爬虫,还慢慢总结了经验,如果对大家有帮助,我将不胜荣幸。如果有什么错误,请及时指出。

[En]

Because this is the first time to use this distributed crawler, but also slowly sum up the experience, if it is helpful to all of you, I would be honored. If there are any mistakes, please point them out in time.

Original: https://blog.csdn.net/weixin_46010646/article/details/119171491
Author: 骡马跪族
Title: scrapy-redis分布式爬虫的使用总结



相关阅读

Title: Python pandas concat 连接 参数 详解 用法 数据拼接 数据堆叠

scrapy-redis分布式爬虫的使用总结

; pd.concat 数据拼接、绑定或堆叠。

一、参数详解

join_axes 已弃用,如果需要使用此功能,建议用 merge

scrapy-redis分布式爬虫的使用总结

scrapy-redis分布式爬虫的使用总结

; 二、实例代码

1. 简单拼接。

1.1 初始化数据及运行结果。
  1. concat 默认是 axis=0 方向连接的。(竖向 / 上下 拼接)。
pd.concat([df1,df2,df3],axis=1) # 横向 / 左右拼接
  1. join 用来指定拼接的方式,可选 inner 和 outer (类似于 merge 的 how 参数)

inner 取交集,舍弃部分,只拼接都存在的列(或者行),数据量可能变小。
outer 取并集。

pd.concat([df1,df2,df3],join='inner')

scrapy-redis分布式爬虫的使用总结
1.2 代码。
import pandas as pd
import numpy as np

df1 = pd.DataFrame({
    'key':['a','b'],
    'data':[1,2]
})
df2 = pd.DataFrame({
    'key':['c','d'],
    'data':[3,4]
})
df3 = pd.DataFrame({
    'key':['e','f','g'],
    'data':[5,6,7]
})

pd.concat([df1,df2,df3])

2. 简单拼接。

2.1 初始化数据及运行结果。
  1. concat 可以用 keys 来标明数据的来源。
  2. 也可以用字典形式。如图注释部分。
  3. 如图,此时我们的索引是 0 1 0 1 2 。我们可以用 ignore_index = True 重新排序。索引就会变为 0 1 2 3 4
pd.concat([df1,df2], keys=['one_data','two_data'],ignore_index = True)

scrapy-redis分布式爬虫的使用总结
2.2 代码。
  1. 小tip:可以用 list('abcdefg')直接产生一个对应的数组。
import pandas as pd
import numpy as np

df1 = pd.DataFrame({
    'key':['a','b'],
    'data':[1,2]
})
df2 = pd.DataFrame({
    'key':list('ace'),
    'data':[3,4,5]
})

pd.concat([df1,df2], keys=['one_data','two_data'])

ignore_index 忽视原来的索引

pd.concat(dfl, ignore_index=True)

读取文件夹下所有文件

import pandas as pd
import glob

files = list(glob.glob('./data/C3.6 数据的拼接与合并/*'))

dfl = []
for f in files:
    df = pd.read_csv(f)
    dfl.append(df)

pd.concat(dfl, join='inner')

Original: https://blog.csdn.net/qq_35240689/article/details/125435164
Author: 正在学习中的李斌
Title: Python pandas concat 连接 参数 详解 用法 数据拼接 数据堆叠

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/353244/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总