我瀏覽了這個網站,我在谷歌上也做了同樣的工作,但是我沒有發現任何關於導出編碼爲UTF-8的csv文件的數據。Scrapy - 編碼csv文件utf-8
我需要編碼我的文件,因爲我有一些法語字符(如É)。 我使用CsvItemExporter,它通常已經在utf-8中編碼,但它不會給我正確的字符。而不是這些字符,我只有像\ A4ybzkzv一些奇怪的東西,我不知道如何有正確的。
我希望我已經清楚了!謝謝你的幫助...
這裏是我的pipelines.py:
# -*- coding: utf-8 -*-
from scrapy import signals
from scrapy.exporters import CsvItemExporter
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
# Define your output file.
class FnacPipeline(CsvItemExporter):
def __init__(self):
self.files = {}
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
f = open('..\\..\\..\\..\\Fnac.csv', 'w').close()
file = open('..\\..\\..\\..\\Fnac.csv', 'w')
self.files[spider] = file
self.exporter = CsvItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
有了這個管道,我有一個錯誤,而不是正確的字符:TypeError: must be str, not bytes
當我改變file = open('..\\..\\..\\..\\Fnac.csv', 'w')
到file = open('..\\..\\..\\..\\Fnac.csv', 'wb')
,我沒有更多的錯誤,但不正確的字符...
輸出我有: France métropolitaine
我想要的輸出: France métropolitaine
快問,你爲什麼不內置scrapy出口商使用?你可以簡單地用'-o'標誌導出你的結果:'scrapy crawl myspider -o results.csv'或者你也可以設置[一些設置來做到這一點](https://doc.scrapy.org/en/latest/主題/饋exports.html)。 – Granitosaurus
@Granitosaurus說過,我認爲一些設置或定製是他正在做的確切的事情。:) – Nabin
因爲我有很多項目,有時在同一個項目中有很多蜘蛛,我想通過蜘蛛創建一個csv文件...我通過可執行文件自動運行我的蜘蛛。所以我不想重新寫我的exe文件,每次我想運行一個蜘蛛:) –