问题
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