如何以正确的顺序打印Scrapy项目键?

时间:2019-06-28 23:18:09

标签: python scrapy web-crawler

例如,我有Scrapy Item:

class QuotetutorialItem(scrapy.Item):
    url = scrapy.Field()
    country = scrapy.Field() 
    state = scrapy.Field()

当我生产此物品时,我会按字母顺序获得字段。

如何使字段顺序与类结构中的顺序相同?

1 个答案:

答案 0 :(得分:1)

您可以使用以下属性custom_settings

class YourSpiderClass(scrapy.Spider):
    name = 'spider_name'
    start_urls = ['http://blahblah.com/']

    custom_settings = {
            'FEED_URI': 'file.csv',
            'FEED_FORMAT': 'csv',
            'FEED_EXPORT_FIELDS': [
                'Url',
                'Country',
                'State',
             ],
         }

    def parse(self, response):
    # you actions