为什么这个条件应该执行为真时执行为假? [英] Why does this condition execute to false when it should execute to true?

查看:44
本文介绍了为什么这个条件应该执行为真时执行为假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的蜘蛛 basic.py 文件中有此代码:

I have this code in my spider basic.py file:

if l.add_xpath('price', '//*[@id="price"]/text()',
            MapCompose(lambda i: i.replace(',', ''), float),
            re = '[,.0-9]'):
    l.add_value('available', 1)
else:
    l.add_value('price', 0)
    l.add_value('available', 0)

当找到价格时,预期结果是 available = 1,但我得到的是 0,我不明白为什么!

The expected result is available = 1 when there is a price found, but what I get is 0 and I don't understand why!

有什么想法吗?

推荐答案

以防万一有人遇到同样的情况:

Just in case someone ran into the same situation:

答案是 add_xpath 不返回任何东西,为了检查是否找到元素,应该使用 get_xpath 代替(见这里).

The answer is that add_xpath does not return anything, and in order to check whether there is an element found or not, get_xpath should be used instead (see here).

这篇关于为什么这个条件应该执行为真时执行为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆