2017-10-19 140 views
0

我有一隻蜘蛛(下圖),我希望能夠每10天左右通過一次Cron作業來運行它,但是,每次我第一次運行它時都會運行它。它重寫字段,而不是僅將項目追加到CSV中的相應字段。我該如何做到這一點,以便無論我運行多少次,頂部只有一組字段標題,並且下面的所有數據都包含在其中。Scrapy CSV輸出重複字段

import scrapy 

class Wotd(scrapy.Item): 
    word = scrapy.Field() 
    definition = scrapy.Field() 
    sentence = scrapy.Field() 
    translation = scrapy.Field() 


class WotdSpider(scrapy.Spider): 
    name = 'wotd' 
    allowed_domains = ['www.spanishdict.com/wordoftheday'] 
    start_urls = ['http://www.spanishdict.com/wordoftheday/'] 
    custom_settings = { 
     #specifies exported fields and their order 
    'FEED_EXPORT_FIELDS': ['word','definition','sentence','translation'] 
    } 

def parse(self, response): 
    jobs = response.xpath('//div[@class="sd-wotd-text"]') 
    for job in jobs: 
     item = Wotd() 
     item['word'] = job.xpath('.//a[@class="sd-wotd-headword-link"]/text()').extract_first() 
     item['definition'] = job.xpath('.//div[@class="sd-wotd-translation"]/text()').extract_first() 
     item['sentence'] = job.xpath('.//div[@class="sd-wotd-example-source"]/text()').extract_first() 
     item['translation'] = job.xpath('.//div[@class="sd-wotd-example-translation"]/text()').extract_first() 
     yield item 

從我一直在閱讀上Scrapy文檔,它看起來像我可以與CsvItemExporter類有勾搭,並設置include_headers_line =假,但我不知道在哪裏添加類在項目結構。

回答

0

首先,您沒有分享您當前的項目結構,因此很難告訴您將具體示例放在哪裏。

我們假設您的項目名爲my_project。在主項目目錄(其中包含settings.py的一個),創建文件exporters.py與此內容:

import scrapy.exporters 

class NoHeaderCsvItemExporter(scrapy.exporters.CsvItemExporter): 
    def __init__(self, file, join_multivalued=', ', **kwargs): 
     super(NoHeaderCsvItemExporter, self).__init__(file=file, include_headers_line=False, join_multivalued=join_multivalued, **kwargs) 

類從標準CSV出口NoHeaderCsvItemExporter繼承,只是指定了我們不希望在輸出中包含標題行。

接下來,您必須爲CSV格式指定新的導出器類,可以在settings.py或spider的custom_settings中指定。按照你目前與後來的選項辦法,那就是:

custom_settings = { 
    'FEED_EXPORT_FIELDS': ['word','definition','sentence','translation'], 
    'FEED_EXPORTERS': { 
     'csv': 'my_project.exporters.NoHeaderCsvItemExporter', 
    } 
} 

要知道,使用這個類,不會有包含在CSV 任何標題行,甚至不是第一個出口。

+0

謝謝,這正是我一直在尋找的。我在沒有更改的情況下運行了一次,以便設置標題,然後進行更改並像魅力一樣工作。謝謝你的幫助! – GainesvilleJesus