将已删除的结果写入CSV文件

时间:2015-08-06 13:25:25

标签: python scrapy scrapy-spider

我想将删除的结果写在CSV文件中,使用Python脚本而不是在终端上传递命令。

以下是代码:

from scrapy.spiders import CrawlSpider, Rule

from scrapy.linkextractors import LinkExtractor

from my_scrapy.items import newsItem

class BbcSpider(CrawlSpider):

    name = "bbcnews"
    allowed_domains = ["bbc.co.uk"]
    start_urls = ["http://www.bbc.co.uk/news/technology/",]
    rules = [Rule(LinkExtractor(allow=['/technology-\d+']), 'parse_story')]

    def parse_story(self, response):

        story = newsItem()

        story['url'] = response.url

        story['headline'] = response.xpath("//title/text()").extract()
        story['intro'] = response.css('p.introduction::text').extract()
        story['p_date'] = response.xpath("//ul/li/div[@class='date date--v2']/text()").extract()
        story['story_body'] = response.css('p.story-body__introduction::text').extract()
        return story

1 个答案:

答案 0 :(得分:0)

您是否尝试过csv模块? https://docs.python.org/2/library/csv.html

import csv
with open('eggs.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

如果没有去写你正在努力的地方。