如何删除csv scrapy中的空格

问题描述 投票:1回答:2

如何删除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)

enter image description here

python python-3.x csv scrapy scrapy-spider
2个回答
0
投票

您可以尝试以下修复:

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',
}

0
投票

我有同样的问题,并自己找到了解决方案:Scrapy python csv output has blank lines between each row

也就是说,我相信在某些方面会有一个补丁。

© www.soinside.com 2019 - 2024. All rights reserved.