【问题标题】:Item caching in ScrapyScrapy 中的项目缓存
【发布时间】:2013-01-22 01:17:01
【问题描述】:

我正在抓取一个结构如下的网站:

Archive
    Article 1
        Authors
            Author 1
            Author 2
        Title
        Body
        Comments
            Comment 1
            Comment 2
    ...

Authors 中的每个作者都有自己的个人资料页面。问题是作者写了多篇文章,所以当我的蜘蛛抓取网站时,我最终会一遍又一遍地抓取相同作者的个人资料。

如何使用 Scrapy 缓存作者个人资料?

【问题讨论】:

  • 我认为scrapy知道之前访问过的链接,所以它不应该抓取已经抓取的页面

标签: python caching web-scraping scrapy web-crawler


【解决方案1】:

你应该像下面的例子一样添加重复过滤器:

from scrapy import signals
from scrapy.exceptions import DropItem

class DuplicatesPipeline(object):

    def __init__(self):
        self.author_ids_seen = set()

    def process_item(self, item, spider):
        if item['author_id'] in self.author_ids_seen:
            raise DropItem("Duplicate item found: %s" % item)
        else:
            self.ids_seen.add(item['author_id'])
            return item

并激活 ITEM_PIPELINES 列表中的 DuplicatesPipeline,

ITEM_PIPELINES = [
    'myproject.pipeline.DuplicatesPipeline',
]

【讨论】:

  • 谢谢,这正是我要找的!
【解决方案2】:

我认为您需要实施新的缓存策略。 see here

另请查看HttpcacheMiddleware

我仍然很困惑为什么它会再次访问访问页面。他们的文档说这是默认策略

此策略不了解任何 HTTP 缓存控制指令。 每个请求及其相应的响应都会被缓存。一样的时候 再次看到请求,返回响应而不传输 任何来自互联网的东西。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-04-14
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2021-01-01
    • 2023-01-09
    相关资源
    最近更新 更多