【问题标题】:The scraped data saved in csv file is not converted into excel(xlsx) file保存在 csv 文件中的抓取数据不会转换为 excel(xlsx) 文件
【发布时间】:2018-07-26 15:51:36
【问题描述】:

这里是网页抓取和从 csv 转换保存的抓取数据的代码 格式化为“.xlsx”文件。

当我使用命令行运行此代码时 - scrapy crawl spider_name -o file_name.csv 通过从抓取数据中提取到 csv 格式,项目文件夹中不会生成excel文件。没有错误信息。请建议如何摆脱这个未知的麻烦。

import os
import scrapy
import csv
import glob
from openpyxl import Workbook

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

    def parse(self, response):
        h1_tag = response.xpath('//h1/a/text()').extract_first()
        tags = response.xpath('//*[@class="tag-item"]/a/text()').extract()

        yield {'H1 Tag': h1_tag, 'Tags': tags}

    def close(self, reason):
        csv_file = max(glob.iglob("*.csv"), key=os.path.getctime)

        wb = Workbook()
        ws = wb.active

        with open(csv_file, 'r') as f:
            for row in csv.reader(f):
                ws.append(row)

        wb.save(csv_file.replace('.csv', '') + '.xlsx')

【问题讨论】:

    标签: python excel csv scrapy


    【解决方案1】:

    试试这个命令:

     scrapy crawl <spider name> -o file.csv -t csv
    

    【讨论】:

    • 这个命令没有效果,同样的事情正在发生。只生成 csv 文件,不生成转换后的 excel 文件。请提出其他解决此问题的方法。
    • 问题解决了。有一个缩进错误没有显示在命令提示符下,感谢您的真诚努力。
    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多