我的刮板蜘蛛中有以下代码,parse方法显然有两个产生,我该怎么做,所以如果scrapy.Request...
发生也不会yield item
for company in self.companies:
for i in range(1, 3):
if item[f"Merchant_{i}"] == company:
yield scrapy.Request(url=item[f"Merchant_{i}_link"],callback=getattr(self, f"parse_{company.lower()}"),meta={'item': item})
yield item
答案 0 :(得分:0)
添加其他标志,例如:
made_request = False
for company in self.companies:
for i in range(1, 3):
if item[f"Merchant_{i}"] == company:
made_request = True
yield scrapy.Request(item[f"Merchant_{i}_link"], getattr(self, f"parse_{company.lower()}"), meta={'item': item})
if not made_request:
yield item
因此,只有在周期内没有完成任何请求的情况下,您的商品才会产生。
答案 1 :(得分:0)
这应该可行,在课堂级别而不是companyFound
内设置parse
class MySpider():
companyFound = False
def parse(self, response):
for company in self.companies:
for i in range(1, 3):
if item[f"Merchant_{i}"] == company:
self.companyFound = True
yield scrapy.Request(item[f"Merchant_{i}_link"], getattr(self, f"parse_{company.lower()}"), meta={'item': item})
if not self.companyFound:
yield item