Я пытаюсь добавить строку в массив indexes внутри функции разбора, но когда я пытаюсь сохранить ее в .json, он становится пустым.
import scrapy
import json
class NewsBrief(scrapy.Spider):
name = "briefs"
indexes = []
def start_requests(self):
ids = []
url = "url"
with open('test_id.json') as json_data:
ids = json.load(json_data)
for i in ids:
yield scrapy.http.FormRequest(url=url+str(i), callback=self.parse)
#self index is empty here
print(self.indexes)
with open('data_briefs.json', 'w') as outfile:
json.dump(self.indexes, outfile)
def parse(self, response):
sentence = ""
for span in enumerate(response.xpath('//div[@class="newsread olnr"]/p/text()').getall()):
sentence += str(span[1]).replace('\n', ' ').replace('\r', ' ')
self.indexes.append(sentence)