简体   繁体   English

AttributeError: 'NoneType' 对象没有属性 'get_text'

[英]AttributeError: 'NoneType' object has no attribute 'get_text'

I am parsing HTML text with我正在解析 HTML 文本

Telephone = soup.find(itemprop="telephone").get_text()

In the case a Telephone number is in the HTML after the itemprop tag, I receive a number and get the output ( "Telephone Number: 34834243244" , for instance).如果电话号码在itemprop标记之后的 HTML 中,我会收到一个号码并获得输出(例如, "Telephone Number: 34834243244" )。

Of course, I receive AttributeError: 'NoneType' object has no attribute 'get_text' in the case no telephone number is found.当然,我收到AttributeError: 'NoneType' object has no attribute 'get_text'在没有找到电话号码的情况下。 That is fine.那没关系。

However, in this case I would like Python not to print an error message and instead set Telephone = "-" and get the output "Telephone Number: -" .但是,在这种情况下,我希望 Python 不打印错误消息,而是设置Telephone = "-"并获得输出"Telephone Number: -"

Can anybody advise how to handle this error?有人可以建议如何处理这个错误吗?

You can easily do that by using try except in Python, It works like: if the given commands in the try block are executed without any error then it never enters the except block, However if there is some error while executing the commands in the try block then it searched for the relevant except handler and executes the commands in corresponding except block.您可以通过在 Python 中使用 try except 轻松做到这一点,它的工作原理如下:如果 try 块中的给定命令在没有任何错误的情况下执行,则它永远不会进入 except 块,但是,如果在 try 中执行命令时出现一些错误block 然后它搜索相关的except处理程序并执行相应的 except 块中的命令。 The common use of try except block is to prevent the program from halting if some issue is encountered. try except 块的常见用途是防止程序在遇到问题时停止。

try:
    Telephone = soup.find(itemprop="telephone").get_text()
except AttributeError:
    print "Telephone Number: -"

You can always use more than one except commands at the same time to handle various exceptions accordingly.您始终可以同时使用多个 except 命令来相应地处理各种异常。

The fully structured exception handling looks something like this:完全结构化的异常处理看起来像这样:

try:
    result = x / y
except ZeroDivisionError:
    print "division by zero!"
else:
    print "result is", result
finally:
    print "executing finally clause"

You can find more about Exception handling and use accordingly您可以找到有关异常处理的更多信息并相应地使用

try:
    Telephone = soup.find(itemprop="telephone").string
except AttributeError:
    print "Telephone Number: -"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-AttributeError:“ NoneType”对象没有属性“ get_text” - Python - AttributeError: 'NoneType' object has no attribute 'get_text' “AttributeError: 'NoneType' object 没有属性 'get_text'” - "AttributeError: 'NoneType' object has no attribute 'get_text'" AttributeError: 'NoneType' object 没有带有输入 id 的属性 'get_text' - AttributeError: 'NoneType' object has no attribute 'get_text' with input id 在亚马逊网页抓取时在 BS4 中收到错误:AttributeError: 'NoneType' 对象没有属性 'get_text' - Receiving an error in BS4 while amazon web scraping : AttributeError: 'NoneType' object has no attribute 'get_text' AttributeError: 'NoneType' 对象在 beautifulsoop web-scraping 中没有属性 'get_text' - AttributeError: 'NoneType' object has no attribute 'get_text' in beautifulsoop web-scraping AttributeError: 'NoneType' 对象没有属性 'get_text' python web-scraping - AttributeError: 'NoneType' object has no attribute 'get_text' python web-scraping AttributeError: 'NoneType' 对象没有属性 'get_text' , id='productTitle is there in URL - AttributeError: 'NoneType' object has no attribute 'get_text' ,id='productTitle is there in URL AttributeError: 'NoneType' 对象没有属性 'get_text' python 3x - AttributeError: 'NoneType' object has no attribute 'get_text' python 3x AttributeError:'NoneType'对象没有属性'get_text',我该如何解决? - AttributeError: 'NoneType' object has no attribute 'get_text' how can I solve it? AttributeError: 'NoneType' 对象没有属性 'get_text'。 BeautifulSoup 对象失败 - AttributeError: 'NoneType' object has no attribute 'get_text'. BeautifulSoup objects failing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM