export python data to csv file

烂漫一生 提交于 2020-01-06 23:49:54

问题


I'm trying to export my file via command line :

scrapy crawl tunisaianet -o save.csv -t csv

but nothing is happenning, any help?

here is my code:

import scrapy
import csv
from tfaw.items import TfawItem


class TunisianetSpider(scrapy.Spider):
    name = "tunisianet"
    allowed_domains = ["tunisianet.com.tn"]
    start_urls = [
        'http://www.tunisianet.com.tn/466-consoles-jeux/',
    ]

    def parse(self, response):
        item = TfawItem()
        data= []
        out = open('out.csv', 'a')
        x = response.xpath('//*[contains(@class, "ajax_block_product")]')
        for i in range(0, len(x)):
            item['revendeur'] = response.xpath('//*[contains(@class, "center_block")]/h2/a/@href').re('tunisianet')[i]
            item['produit'] = response.xpath('//*[contains(@class, "center_block")]/h2/a/text()').extract()[i]
            item['url'] = response.xpath('//*[contains(@class, "center_block")]/h2/a/@href').extract()[i]
            item['description'] = response.xpath('//*[contains(@class, "product_desc")]/a/text()').extract()[i]
            item['prix'] = response.xpath('//*[contains(@class, "price")]/text()').extract()[i]
            data = item['revendeur'], item['produit'], item['url'], item['description'], item['prix']
            yield data
            out.write(str(data))
            out.write('\n')

回答1:


I assume you are getting these errors:

ERROR: Spider must return Request, BaseItem, dict or None, got 'tuple' in <GET http://www.tunisianet.com.tn/466-consoles-jeux>

which specifically says what's wrong, you are returning tuples as items, change your yield code to:

...
item['prix'] = response.xpath('//*[contains(@class, "price")]/text()').extract()[i]
yield item


来源:https://stackoverflow.com/questions/36943255/export-python-data-to-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!