【发布时间】:2020-12-01 02:33:52
【问题描述】:
代码:
int(root.find('my_number').text) if root.find('my_number') is not None else None
如果找到该值,则预期输出应该是数字类型,但如果找不到该值,则应该是无。但是,我面临以下TypeError:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
如果满足my_number 存在的条件,该语句不应该只转换为int() 吗?
【问题讨论】:
-
root.find("my_number")存在,但.text属性为None。 -
也许数字在属性中,而不是文本中?你能展示你的 XML 吗?
-
@Aplet123 是的!似乎对于那个字段我需要 .text 来检查它是否为无。谢谢!
标签: python python-3.x beautifulsoup