AttributeError: 'JsonExporterPipeline' object has no attribute 'exporter'

来源:5-2 课程如何应对网站反爬变化?

秦垚

2023-05-08

老师,你好,我在测试JsonExporterPipline是报错,请帮我看一下吧。
这是pipelines.py的代码:

class JsonExporterPipeline(object):
    """
    调用scrapy提供的json export导出json文件
    """
    def __int__(self):
        self.file = open("article_export.json", "wb")
        self.exporter = JsonItemExporter(self.file, encoding="utf-8", ensure_ascii=False)
        self.exporter.start_exporting()

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

这是settings.py的代码:

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   # "MonthlyLawSpider.pipelines.JsonWithEncodingPipeline": 1,
   "MonthlyLawSpider.pipelines.JsonExporterPipeline": 1,
   "MonthlyLawSpider.pipelines.MysqlPipline": 2,
   "MonthlyLawSpider.pipelines.MonthlylawspiderPipeline": 300,

}

这是报错信息:

Traceback (most recent call last):
  File "D:\AllProjects\SpyderProjects\MonthlyLawSpider\venv\Lib\site-packages\twisted\internet\defer.py", line 892, in _runCallbacks
    current.result = callback(  # type: ignore[misc]
  File "D:\AllProjects\SpyderProjects\MonthlyLawSpider\venv\Lib\site-packages\scrapy\utils\defer.py", line 304, in f
    return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
  File "D:\AllProjects\SpyderProjects\MonthlyLawSpider\MonthlyLawSpider\MonthlyLawSpider\pipelines.py", line 82, in process_item
    self.exporter.export_item(item)
AttributeError: 'JsonExporterPipeline' object has no attribute 'exporter'
写回答

1回答

bobby

2023-05-10

//img.mukewang.com/szimg/645a6e6e09caaec209620254.jpg 这里的82行代码 是哪一段代码?

0
2
bobby
回复
秦垚
你留个qq 我加你看看
2023-05-15
共2条回复

Scrapy打造搜索引擎 畅销4年的Python分布式爬虫课

带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎

5818 学习 · 6291 问题

查看课程