V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
m939594960
V2EX  ›  问与答

pyspider 把一个方法里获取的至传到另一个方法中

  •  
  •   m939594960 · 2015-06-23 17:51:43 +08:00 · 3602 次点击
    这是一个创建于 3291 天前的主题,其中的信息可能已经有所发展或是发生改变。

    官方手册里这样写的

    def on_start(self):
        self.crawl('http://www.example.org/', callback=self.callback,
               save={'a': 123})
    def callback(self, response):
        return response.save['a']
    

    我是这样写的。

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('http://baidu.com', callback=self.index_page,
               save={'a': 123 })
    
    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        print response.save['a']
        for each in response.doc('a[href^="http"]').items():
            self.crawl(each.attr.href, callback=self.detail_page)
    

    报错
    [E 150623 17:50:22 basehandler:193] 'NoneType' object has no attribute 'getitem'
    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/pyspider/libs/base_handler.py", line 186, in run_task
    result = self._run_task(task, response)
    File "/usr/local/lib/python2.7/dist-packages/pyspider/libs/base_handler.py", line 166, in _run_task
    return self._run_func(function, response, task)
    File "/usr/local/lib/python2.7/dist-packages/pyspider/libs/base_handler.py", line 148, in _run_func
    return function(*arguments[:len(args) - 1])
    File "<taobao>", line 20, in index_page
    TypeError: 'NoneType' object has no attribute '
    getitem_'

    这是为何 是我做错了什么?
    
    3 条回复    2015-06-24 19:19:46 +08:00
    binux
        1
    binux  
       2015-06-23 19:16:58 +08:00
    无法复现
    orzfly
        2
    orzfly  
       2015-06-23 19:43:46 +08:00
    实际跑 index_page 的时候,你用的数据可能是之前没加上 save 字段时 crawl 存进数据库的任务吧?每一个 crawl 都是存进数据库的,然后他的 callback 是稍后从数据库里读出来运行的。如果你存进数据库的时候 save 字段并没有值,那你后面运行到那条任务的时候自然是会出错的。

    也许你需要清空一下任务数据库再来……
    binux
        3
    binux  
       2015-06-24 19:19:46 +08:00
    @orzfly 有道理。。
    在页面上调试的时候也是这样。运行到 Index_page 这一步的时候,task 已经生成了,这时候修改 on_start 的内容,并没有让 save 参数带入 task。
    需要返回上一步 on_start,重新点击进入 index_page。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5056 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:13 · PVG 15:13 · LAX 00:13 · JFK 03:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.