我无法将表格保存到csv文件中

时间:2020-07-12 15:29:31

标签: python web-scraping

我想用scrappy从Wikipedia抓取一个表,问题是我无法将其另存为csv文件。 这是我的代码

import scrapy 

class DCspider( scrapy.Spider ):
    name = 'ufcspider'
    def start_requests(self):
        url_short = "https://en.wikipedia.org/wiki/Ultimate_Fighting_Championship_rankings"
        yield scrapy.Request(url = url_short,
                         callback = self.parse)
    def parse(self, response):
        # Create an extracted list of the table 
        table = response.xpath('//table[contains(@class,"wikitable")]/text()').extract()
        filepath = "ufc.csv"
        with open(filepath, "w") as f:
            f.writelines([parragraphs + "\n" 
            for parragraphs in table])

1 个答案:

答案 0 :(得分:0)

您可以使用csv库写入csv文件。以下代码可以为您提供帮助:

import scrapy 
import csv

class DCspider( scrapy.Spider ):
    name = 'ufcspider'
    def start_requests(self):
        url_short = "https://en.wikipedia.org/wiki/Ultimate_Fighting_Championship_rankings"
        yield scrapy.Request(url = url_short,
                     callback = self.parse)
    def parse(self, response):
        # Create an extracted list of the table 
        table = response.xpath('//table[contains(@class,"wikitable")]/text()').extract()
        filepath = "ufc.csv"
        with open(filepath, "w") as f:
            writer = csv.writer(f, delimiter=',')
            for parragraph in table:
                writer.writerow([parragraph + "\n"])