У меня есть паук scrapy, который выдает запросы в двух циклах for, есть ли способ сделать его разрывом цикла for в зависимости от результатов функции yield?(То есть разрыв, когда parse_results
переменная x
найдена)
def parse(self,response):
#code here
offsets = [i for i in range(0,10001,20)]
for query in all_queries:
for offset in offsets:
query = query+f'&offset={offset}'
yield scrapy.Request(url= query,callback= self.parse_results)
#break if parse_results gives KeyError (x is True)
def parse_results(self,response):
try:
x=response.xpath('//*[@id="alert_bar"]').extract_first()
if x:
raise KeyError # condition
except KeyError:
rows = response.xpath('//*[@id="sRes"]/div[@class="sResCont"]')
for row in rows:
if row.xpath('div[@class="adFrameCnt"]').extract_first():
continue
else:
item = UserItem() # scrapy item
item['username'] = row.xpath('div/div[@class="sResMain"]/b/a/text()').extract_first()
item['link'] = response.urljoin(row.xpath('div/div[@class="sResMain"]/b/a/@href').extract_first())
self.found_items.append(item)