my_ext.py 575 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from scrapy.exporters import BaseItemExporter


class TXTItemExporter(BaseItemExporter):

    def __init__(self, file, **kwargs):
        super().__init__(dont_fail=True, **kwargs)
        self.file = file

    def export_item(self, item):
        # _get_serialized_fields 方法可以获得 item 所有字段,并返回迭代器
        print(self._get_serialized_fields(item, default_value=''))
        print(self.file)
        for name, value in self._get_serialized_fields(item, default_value=''):
            self.file.write(bytes("\nname:" + value, encoding="utf-8"))