首页 > 解决方案 > 无法将值转换为 int python

问题描述

代码:

int(root.find('my_number').text) if root.find('my_number') is not None else None

如果找到此值,则预期输出应该是数字类型,但如果找不到该值,则应该是 None。但是,我面临以下问题TypeError

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

该语句不应该只转换为int()是否满足我的条件my_number存在吗?

标签: pythonpython-3.xbeautifulsoup

解决方案


您应该检查 root.find() 方法的返回值是什么


推荐阅读