python isinstance未按预期工作

2024-03-29 12:55:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python2.7中使用scrapy0.20

这是我的密码

def process_spider_output(self, response, result, spider):
    print 'process_spider_output'
    for r in result:
        print 'r type = {0}'.format(type(r))
        if isinstance(r, Request):
            key = self._get_key(r)
            if self.db.has_key(key):
                spider.log("Ignoring already visited: %s" % r, level=log.INFO)
                print 'Ignoring already visited: {0}'.format(r)
                continue
        elif isinstance(r, FlexibleItem):
            key = self._get_key(response.request)
            self.db[key] = str(time.time())
            spider.log("Writing to log: %s" % key, level=log.INFO)
            print 'Writing to log {0}'.format(s)
        yield r

我正在尝试打印r的类型,我的cmd上的结果是:

r type = <class 'TestSpider.spiders.FlexibleItem.FlexibleItem'>

因此类型为FlexibleItem

但为什么第二个条件永远不会成为现实呢?我从来没见过第二份报告的打印情况

编辑

这张图片是为好心的用户谁正在帮助我现在。在

enter image description here


Tags: keyselflogformatoutputgetifresponse
2条回答

有两种可能性:

  1. FlexibleItem实际上不是{}。请确保没有两种导入名称的方法。

  2. TestSpider.spiders.FlexibleItem.FlexibleItemRequest的后代。如果是这样的话,if语句将为true,elif语句将永远不会求值。

在这种情况下,要么是伊格纳西奥说的,要么是对问题的描述不正确。在

but why the second condition is never becomes true? I have never seen the printing statement from the second condition

你确定第二个条件永远不会变成真的吗?把1/0放在elif isinstance(r, FlexibleItem):后面,以确保它的正确性。在

在任何情况下,找出发生了什么的一个更好的方法是使用调试器。{{cd3>将在第一步之前运行

相关问题 更多 >