1
如果返回None
或请求回调中的空列表 - 将终止刮取。如果我只想忽略此请求并继续处理预定请求,该怎么办?如何从回调中返回空结果并继续刮取?
如果返回None
或请求回调中的空列表 - 将终止刮取。如果我只想忽略此请求并继续处理预定请求,该怎么办?如何从回调中返回空结果并继续刮取?
你有2种选择:
return
#or
return None
#to interrupt parse function execution
return
#to return None for current item and continue parse function execution
yield None
# or just do nothing
您需要使用scrapy例外DropItem
在处理项目在物品管道中。
检查在Scrapy文档中指定的example。例如,如果需要,可以使用例如以下的方法来制造一种或多种其他材料。来自文档的示例代码示例。
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)
你有一些示例代码吗? – johntellsall
@shavenwarthog我错了 - 刮被终止,因为蜘蛛试图抓取域不是'allowed_domains'属性的网址。 –
@GillBates - 你有没有考虑过使用Drop Item http://doc.scrapy.org/en/latest/topics/exceptions.html#dropitem? – Girish