跳转到主要内容

Scrapy内置ItemLoader的替代方案,专注于回退解析器的可维护性。

项目描述

https://img.shields.io/pypi/pyversions/scrapy-loader-upkeep.svg https://img.shields.io/pypi/v/scrapy-loader-upkeep.svg https://travis-ci.org/BurnzZ/scrapy-loader-upkeep.svg?branch=master https://codecov.io/gh/BurnzZ/scrapy-loader-upkeep/branch/master/graph/badge.svg

概述

这通过增加关注蜘蛛随时间可维护性的功能,在Scrapy的内置ItemLoader上进行了改进。

这允许开发者跟踪解析器在爬取过程中的使用频率,从而安全地删除过时的css/xpath回退规则。

动机

Scrapy默认支持在ItemLoader中添加多个css/xpath规则,以便为开发者提供一种方便的方式,以跟上网站的变化。

然而,一些网站的布局变化比其他网站更频繁,而有些网站进行A/B测试长达数周/数月,开发者需要适应这些变化。

这些回退css/xpath规则很快就会过时,并使项目充满潜在的无效代码,对蜘蛛的长期维护构成威胁。

原始想法提案:https://github.com/scrapy/scrapy/issues/3795

用法

from scrapy_loader_upkeep import ItemLoader

class SiteItemLoader(ItemLoader):
    pass

在蜘蛛回调中使用它的样子

def parse(self, response):
    loader = SiteItemLoader(response=response, stats=self.crawler.stats)

使用此ItemLoader时,除了注入到其中的统计依赖部分外,不会改变其使用方式,这是跟踪解析器规则使用所必需的。

此功能仅适用于以下 ItemLoader 方法

  • add_css()

  • replace_css()

  • add_xpath()

  • replace_xpath()

基本爬虫示例

此示例来自 examples/ 目录。

$ scrapy crawl quotestoscrape_simple_has_missing

此内容应在统计信息中输出

2019-06-16 14:32:32 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{ ...
  'parser/QuotesItemLoader/author/css/1': 10,
  'parser/QuotesItemLoader/quote/css/1/missing': 10,
  'parser/QuotesItemLoader/quote/css/2': 10
  ...
}

在此示例中,我们可以看到,在抓取过程中,针对 quote 字段的第一个 css 规则并未匹配到任何实例。

新功能

与上面的示例一样,我们仅限于在执行过程中调用 add_css()add_xpath() 等时的位置上下文。

在某些情况下,开发者可能会维护一个包含大量不同解析器的蜘蛛,以处理网站中的不同布局。了解解析器的作用或用途将很有意义。

支持新的可选 name 参数,以提供有关特定解析器的更多上下文。这支持两种创建回退解析器的主要类型

  1. 多次调用

loader.add_css('NAME', 'h1::text', name='Name from h1')
loader.add_css('NAME', 'meta[value="title"]::attr(content)', name="Name from meta tag")

将产生类似的结果

{ ...
  'parser/QuotesItemLoader/NAME/css/1/Name from h1': 8,
  'parser/QuotesItemLoader/NAME/css/1/Name from h1/missing': 2,
  'parser/QuotesItemLoader/NAME/css/2/Name from meta tag': 7,
  'parser/QuotesItemLoader/NAME/css/2/Name from meta tag/missing': 3,
  ...
}
  1. 单次调用中的分组解析器

loader.add_css(
    'NAME',
    [
        'h1::text',
        'meta[value="title"]::attr(content)',
    ],
    name='NAMEs at the main content')
loader.add_css(
    'NAME',
    [
        'footer .name::text',
        'div.page-end span.name::text',
    ],
    name='NAMEs at the bottom of the page')

将产生类似的结果

{ ...
  'parser/QuotesItemLoader/NAME/css/1/NAMEs at the main content': 8,
  'parser/QuotesItemLoader/NAME/css/1/NAMEs at the main content/missing': 2,
  'parser/QuotesItemLoader/NAME/css/2/NAMEs at the main content': 7,
  'parser/QuotesItemLoader/NAME/css/2/NAMEs at the main content/missing': 3,
  'parser/QuotesItemLoader/NAME/css/3/NAMEs at the bottom of the page': 8,
  'parser/QuotesItemLoader/NAME/css/3/NAMEs at the bottom of the page/missing': 2,
  'parser/QuotesItemLoader/NAME/css/4/NAMEs at the bottom of the page': 7,
  'parser/QuotesItemLoader/NAME/css/4/NAMEs at the bottom of the page/missing': 3,
  ...
}

后者在将回退解析器按页面中的布局/排列分组时很有用。

要求

Python 3.6+

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定要选择哪个,请了解有关 安装软件包 的更多信息。

源代码发行版

scrapy-loader-upkeep-0.1.1.tar.gz (5.6 kB 查看散列)

上传时间 源代码

构建发行版

scrapy_loader_upkeep-0.1.1-py3-none-any.whl (6.4 kB 查看散列)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面