如何删除 csv scrapy 中的空格

How do I remove the blank spaces in my csv scrapy

如何删除 csv 中的空格?

我运行:scrapy crawl quotes -o quotes.csv。输出结果如图所示。

我知道这是一个 windows 问题,因为在 windows 上处理 csv 时我必须使用下面的代码。例如在使用硒时。

with open('C:\fa.csv', 'a+', newline='', encoding="utf-8") as outfile:

Scrapy 处理 Csv 的方式不同,我用

启动
scrapy crawl quotes -o quotes.csv

There is no: scrapy crawl quotes -o /n quotes.csv

代码:

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
    ]

    def parse(self, response):
        for quote in response.css('div.quote'):
            yield {
                'text': quote.css('span.text::text').extract_first(),
                'author': quote.css('small.author::text').extract_first(),
                'tags': quote.css('div.tags a.tag::text').extract(),
            }

        next_page = response.css('li.next a::attr(href)').extract_first()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

您可以尝试以下修复方法:

from scrapy.conf import settings
from scrapy.contrib.exporter import CsvItemExporter


class FixCsvItemExporter(CsvItemExporter):

    def __init__(self, *args, **kwargs):
        newline = settings.get('CSV_NEWLINE', '')
        kwargs['newline'] = newline
        super(FixCsvItemExporter, self).__init__(*args, **kwargs)

然后,在你的爬虫目录下的settings.py文件中你需要添加:

FEED_EXPORTERS = {
    'csv': 'path.to.sourcefile.FixCsvItemExporter',
}

我遇到了同样的问题,自己找到了解决方案:

也就是说,我相信某个时候会有补丁。