【发布时间】:2019-06-26 20:46:17
【问题描述】:
Scrapy 文档列出了所有the built-in methods of ItemLoader instances 并解释了how to declare your own Item Loaders。但是,您声明的任何 ItemLoaders 都将适用于所有已处理的项目。您可以使用 Item Loader Contexts 稍微修改他们的行为,但这通常不够精细。
假设我有一个 Scrapy 项目,其中蜘蛛和项目都继承了相同的基础蜘蛛和项目加载器,但蜘蛛都包含特定于站点的逻辑和一些常用功能。我在 Scrapy 文档中没有提到向 ItemLoaders 添加类方法,而不是:
import mymodule
class MySpider(BaseSpiderName):
def parse_item(self, response):
product = ItemLoader(item=Product(), response=response)
new_value = mymodule.myfunction(argument, ..., ...)
product.add_value('my_field', new_value)
你可以写:
# (no extra import)
class MySpider(BaseSpiderName):
def parse_item(self, response):
product = CustomItemLoader(item=Product(), response=response)
product.custom_function(argument, ..., ...)
尽管这似乎是扩展 ItemLoaders 的一种显而易见的方法,就像您对任何其他类所做的那样,但它没有记录在案,而且我在我检查过的任何地方(Google、StackOverflow)都没有看到如何在 Scrapy 中执行此操作的示例。是否可能/受支持,您将如何声明它们?
【问题讨论】: