Scrapy内置ItemLoader的替代方案,专注于回退解析器的可维护性。
项目描述
概述
这通过增加关注蜘蛛随时间可维护性的功能,在Scrapy的内置ItemLoader上进行了改进。
这允许开发者跟踪解析器在爬取过程中的使用频率,从而安全地删除过时的css/xpath回退规则。
动机
Scrapy默认支持在ItemLoader中添加多个css/xpath规则,以便为开发者提供一种方便的方式,以跟上网站的变化。
然而,一些网站的布局变化比其他网站更频繁,而有些网站进行A/B测试长达数周/数月,开发者需要适应这些变化。
这些回退css/xpath规则很快就会过时,并使项目充满潜在的无效代码,对蜘蛛的长期维护构成威胁。
用法
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 参数,以提供有关特定解析器的更多上下文。这支持两种创建回退解析器的主要类型
多次调用
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,
...
}
单次调用中的分组解析器
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 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | e379506334e510d36adeaab6571993637d89951696aa2959f35ccdc34b2e7888 |
|
MD5 | 3853153add06d5c414004be58178d0a7 |
|
BLAKE2b-256 | d712de44117f7ac5b61bb1a43c57d022b928c4423ab98a015639696d3e980386 |
scrapy_loader_upkeep-0.1.1-py3-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 0cafb6ebf3f89a370d3d332e6f3baf4e986452351bd9f1840b7c48f02ffee670 |
|
MD5 | 71bbe42dc2fa28577b94a64248856895 |
|
BLAKE2b-256 | bb57349bb8a9ed72961e03161ea074d97f5ec285559a3424a22897046f5cfc7c |