Python 如何从回调返回空结果并继续刮取?
如果从请求回调返回Python 如何从回调返回空结果并继续刮取?,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,如果从请求回调返回None或空列表,则将终止刮取。如果我想忽略此请求并继续处理计划请求,该怎么办?您有两个选项: 如果你没有使用收益率 如果你使用了收益率 在处理项目管道中的项目时,需要使用刮擦异常 检查刮片文档中指定的 e、 g。文档中的示例代码示例 from scrapy.exceptions import DropItem class PricePipeline(object): vat_factor = 1.15 def process_item(self, item
None
或空列表,则将终止刮取。如果我想忽略此请求并继续处理计划请求,该怎么办?您有两个选项:
如果你没有使用收益率
如果你使用了收益率
在处理项目管道中的项目时,需要使用刮擦异常 检查刮片文档中指定的 e、 g。文档中的示例代码示例
from scrapy.exceptions import DropItem
class PricePipeline(object):
vat_factor = 1.15
def process_item(self, item, spider):
if item['price']:
if item['price_excludes_vat']:
item['price'] = item['price'] * self.vat_factor
return item
else:
raise DropItem("Missing price in %s" % item)
您有一些示例代码吗?@shavenwarthog我搞错了-由于蜘蛛试图从不在
允许的\u域
属性中的域爬网url,所以刮取被终止。@GillBates-您考虑过使用拖放项吗?
#to interrupt parse function execution
return
#to return None for current item and continue parse function execution
yield None
# or just do nothing
from scrapy.exceptions import DropItem
class PricePipeline(object):
vat_factor = 1.15
def process_item(self, item, spider):
if item['price']:
if item['price_excludes_vat']:
item['price'] = item['price'] * self.vat_factor
return item
else:
raise DropItem("Missing price in %s" % item)