Проблема с обновлением словаря
Проблема заключается в том, что при переходе по ссылке(parse_more) хочу, чтобы словарь обновлялся и добавлялся с список. Получается список из словарей
Выходит так что parse() добавляет, как надо, а при добавлении parse_mode(), все идет не по плану
Пробовал метод update(), не сработало, решил поизвращаться, но не спасло
Что можете посоветовать? Может есть другие пути к решению?
# -*- coding: utf-8 -*-
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.settings import Settings
from scrapy.http import HtmlResponse
import csv
# global itemsfor_item
items = []
for_items = {}
class FunpaySpider(scrapy.Spider):
name = 'funpay'
allowed_domains = ['funpay.ru']
start_urls = ['https://funpay.ru/']
def parse(self, response: HtmlResponse):
for game_info in response.xpath('//div[@class="game-title"]//a'):
game_title = game_info.xpath('./text()').get()
link = game_info.xpath('./@href').get()
items.append({'game_title': game_title,
'link': link})
more_url = game_info.urljoin(link)
response.follow(more_url, self.parse_more)
def parse_more(self, response: HtmlResponse):
description = response.xpath('.//div[@class="block-info"]/p/text()')
items[-1]['description'] = description
if __name__ == '__main__':
process = CrawlerProcess(Settings())
process.crawl(FunpaySpider)
process.start()
print(items)
with open('FunPay.csv', 'w', encoding='utf-8', newline='') as file:
pen = csv.writer(file)
pen.writerow(('url', 'game_title', 'description'))
for item in items:
pen.writerow((item['link'], item['game_title'], item['description']))