我想在 scrapy 下载中间件用 BloomFilter 去重, 思路是将数据库之前下载过的正文页 url 添加到 BloomFilter(), 判断是否存在 目前遇到的问题是启动爬虫, 定义的 start_url 已经加到 BloomFilter(),不能往下执行 代码如下 from bloom_filter import BloomFilter from scrapy import signals import pymysql import pandas as pd from scrapy.conf import settings from scrapy.exceptions import IgnoreRequest
have_met = BloomFilter() #实例布隆容器 class JianshuSpiderMiddleware(object): def init(self): self.conn = pymysql.connect(host=settings['MYSQL_HOST'], database=settings['MYSQL_DB'],user=settings['MYSQL_USER'], password=settings['MYSQL_PWD'], charset="utf8") self.cur = self.conn.cursor() self.counter = 0 sql = 'SELECT url FROM jianshu_1;' df = pd.read_sql(sql, self.conn) for url in df['url'].get_values(): "将数据库中上次爬取的 url 添加" have_met.add(url)
def process_request(self, request, spider): if request.url in have_met: print('-------已经存在---------') raise IgnoreRequest() else: return None
不知哪位大神可否解答,十分感谢。
1
soms OP 首次提问,不懂代码排版,,,,各位大佬见谅
|
2
achiles 2017-11-28 19:55:21 +08:00
我就不信这排版有人会看
|