繁体   English   中英

如何以特定顺序将数据导出到csv文件

[英]how to export data to csv file in specific order

我找到了几种有关如何通过重写OrderItem按特定顺序提取数据的解决方案

class OrderedItem(scrapy.Item):
    def __init__(self, *args, **kwargs):
        self._values = OrderedDict()
        if args or kwargs:
            for k, v in six.iteritems(dict(*args, **kwargs)):
                self[k] = v

我有更多的数据正在提取,并且每次订单不同时def repr (self):返回json.dumps(OrderedDict(self),sure_ascii = False)

class NewItem(OrderedItem):
    title = scrapy.Field()
    price = scrapy.Field()

然后在NewItem器脚本中,我定义了NewItem对象的实例

def parse(self, response):
        items = NewItem()
        items['title'] = response.xpath(
"//span[@class='pdp-mod-product-badge-title'/text()").extract_first()
        items['price'] = response.xpath("//span[contains(@class, 'pdp-price')]/text()").extract_first()

        yield items

您需要在settings.py定义您的订单:

FEED_EXPORT_FIELDS = ["title", "price"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM