小编RC_*_*ata的帖子

AttributeError:“ NoneType”对象没有属性“ get_text”

我正在解析HTML文本

Telephone = soup.find(itemprop="telephone").get_text()
Run Code Online (Sandbox Code Playgroud)

如果在itemprop标记后的HTML中有一个电话号码,我会收到一个号码并获取输出("Telephone Number: 34834243244"例如)。

当然,AttributeError: 'NoneType' object has no attribute 'get_text'如果找不到电话号码,我会收到。那样就好。

但是,在这种情况下,我希望Python不打印错误消息,而是设置Telephone = "-"并获取输出"Telephone Number: -"

有人可以建议如何处理此错误吗?

python attributeerror

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

attributeerror ×1

python ×1