我正在检查 Scrapy 中关于它的文档: https ://docs.scrapy.org/en/latest/topics/items.html#dataclass-objects
from dataclasses import dataclass
@dataclass
class CustomItem:
one_field: str
another_field: int
不幸的是,他们没有提供任何关于如何在蜘蛛中使用它的示例。我正在使用这种语法定义我的scrapy项目,我在我的代码中以这种方式调用:
product = ProductItem(
publicName=response.xpath(
'//title/text()').re(r"(.+?)(?=( v?(CC-V)?(\d+\.?){1,3}))")[0],
description=None,
productType=self.get_product_type(response),
salesPage=response.xpath(
'normalize-space(//div[@class="sales_page"]/center/a/@href)').get(),
tags=[],
developer=response.xpath(
'//div[@class="website"]/a/div/div/div[@class="head"]/text()').get(),
developerId=response.xpath(
'//div[@class="fox-sidebar"]/div[4]/div/ul/li/span/a[@rel="tag"]/@href').re(r"\/vendor\/(.*)\/")[0]
)
我收到此错误:
TypeError: 'ProductItem' object is not iterable
PS:我希望收到一个错误,例如,当使用 scrapy 预期整数时将类值定义为字符串。