Overriding the serialize_field() method in Scrapy

Deadly 提交于 2021-01-29 10:50:19

问题


Im using code from Scrapy documentation, with "Product" class item created

from scrapy.exporter import XmlItemExporter

class ProductXmlExporter(XmlItemExporter):

    def serialize_field(self, field, name, value):
        if field == 'price':
            return f'$ {str(value)}'
        return super(Product, self).serialize_field(field, name, value)

and always get error from command line

return super(Product, self).serialize_field(field, name, value)
TypeError: super(Product, obj): obj must be an instance or subtype of type

I am new to Python so any help would be appreciated and did some research on "super" function but still dont understand how to connect this class inside code in this example.


回答1:


Not familiar with scrapy but how is the Product class related to ProductXmlExporter? The super keyword does not accept unrelated classes so your call should really be return super(XmlItemExporter, self).serialize_field(field, name, value), assuming you want to call serialize_field method of XmlItemExporter.



来源:https://stackoverflow.com/questions/64396610/overriding-the-serialize-field-method-in-scrapy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!