scrapy 2.3 无价格的价格验证和删除项目
让我们看看下面的假设管道,它调整了 price 不包括增值税的项目的属性 (price_excludes_vat 属性),并删除不包含价格的项目:
from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem
class PricePipeline:
vat_factor = 1.15
def process_item(self, item, spider):
adapter = ItemAdapter(item)
if adapter.get('price'):
if adapter.get('price_excludes_vat'):
adapter['price'] = adapter['price'] * self.vat_factor
return item
else:
raise DropItem(f"Missing price in {item}")