V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
soms
V2EX  ›  Python

scrapy bloom_filter 去重

  •  
  •   soms · 2017-11-28 15:16:37 +08:00 · 1676 次点击
    这是一个创建于 2358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想在 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

    不知哪位大神可否解答,十分感谢。

    soms
        1
    soms  
    OP
       2017-11-28 15:20:41 +08:00
    首次提问,不懂代码排版,,,,各位大佬见谅
    achiles
        2
    achiles  
       2017-11-28 19:55:21 +08:00
    我就不信这排版有人会看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1097 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:42 · PVG 06:42 · LAX 15:42 · JFK 18:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.