我有一个spider(如下),我希望能够通过Cron作业每10天左右运行一次,但是,每次我运行它之后,第一次。它重写字段,而不是仅仅将项目附加到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
从我在垃圾文档上读到的内容来看,我可能需要处理csviteExporter类和设置include_headers_line=False,但我不确定该在项目结构中的何处添加该类。在
首先,您没有分享您当前的项目结构,因此很难建议将其放在具体示例中的位置。在
假设您的项目名为
my_project
。在主项目目录(包含settings.py
)下,使用以下内容创建文件exporters.py
:类
NoHeaderCsvItemExporter
继承自标准CSV导出器,只是指定我们不希望在输出中包含头行。在接下来,您必须为CSV格式指定新的导出器类,可以是
^{pr2}$settings.py
,也可以是spider的custom_settings
。按照您当前的方法和后面的选项,它将是:请注意,使用这个类,CSV中不会包含任何标题行,甚至第一次导出时也不会。在
相关问题 更多 >
编程相关推荐