0

I'm working on a crawler and I have to save the output in a csv file.

Here is my code:

import scrapy

class ArticleSpider(scrapy.Spider):
    name = "article"

    def start_requests(self):
        urls = [
        'https://www.topart-online.com/de/Ahorn-japan.%2C-70cm%2C--36-Blaetter----Herbst/c-KAT282/a-150001HE'   
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.url.split("/")[-1]
        filename = 'article-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

    def parse(self, response):
        yield{
            'title': response.xpath('//h1[@class="text-center text-md-left mt-0"]/text()').get(),
            'quantity': response.xpath('//div[@class="col-6"]/text()')[0].get().strip(),
            'delivery_status': response.xpath('//div[@class="availabilitydeliverytime"]/text()').get().replace('/','').strip(),
            'itemattr': response.xpath('//div[@class="productcustomattrdesc word-break col-6"]/text()').getall(),
            'itemvalues': response.xpath('//div[@class="col-6"]/text()').getall()
        }

My question is: How can I output itemattr and itemvalues in the correct order? So I can see for example: Umkarton(itemattr) 20/20/20(dimension of a Umkarton)

Ryan
  • 2,167
  • 2
  • 28
  • 33
y.y
  • 308
  • 4
  • 16
  • I don't understand what you mean by correct order, could you show whats the order now (*wrong*) and the order you want? Also, you have two `parse` methods, one will overwrite the other, only the latter will execute. – renatodvc Jul 23 '20 at 13:05
  • thanks renatodvc, ill correct my parse methods later and then i am going to be more accurate with my question – y.y Jul 23 '20 at 14:10
  • Please follow the link https://stackoverflow.com/questions/55851125/how-to-sort-the-scrapy-item-info-in-customized-order , It may help you. – Samsul Islam Jul 23 '20 at 14:42

0 Answers0