[英]export python data to csv file
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')
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.