继承自BaseHander的类,而非直接继承BaseHandler,引用失败

发布于 2022-09-04 20:10:27 字数 1486 浏览 14 评论 0

运行代码如下:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-04-19 12:07:53
# Project: http

from pyspider.libs.base_handler import *
import json
import time

class BBSBaseHandler(BaseHander):
    '''this class write into a file'''
    def _on_cronjob(self, response, task):
        # add custom variables at the beginning of starting job
        self._start=time.time()
        super(BBSBaseHandler, self)._on_cronjob(respnse, task)
    
    def on_finished(self, response, task):
        # use custom variables when job endding
        tick = time.tiem()-self._start
        
        

class Handler(BBSBaseHandler):
    '''inherit class from a file'''
    crawl_config = {
    }

    @every(minutes=1)
    def on_start(self):
        url = 'http://httpbin.org/get'
        self.crawl(url, callback=self.index_page)

    @config(0)
    def index_page(self, response):
        return json.load(respnse.content)
        

报错:

Traceback (most recent call last):
  File "/home/kidd/www/pyspider/pyspider/processor/project_module.py", line 51, in build_module
    module = loader.load_module(project['name'])
  File "/home/kidd/www/pyspider/pyspider/processor/project_module.py", line 176, in load_module
    six.exec_(code, mod.__dict__)
  File "<http>", line 9, in <module>
NameError: name 'BaseHander' is not defined

想要的结果:
例如在项目外部一文件, 继承自BaseHander, 计算运行所耗时间.
当然不只是计算耗时.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2022-09-11 20:10:27

BaseHandler

另外,你不能在类中使用成员变量,pyspider 设计为分布式运行,不保证使用同一个实例运行。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文