I am parsing HTML text with
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).
Of course, I receive AttributeError: 'NoneType' object has no attribute 'get_text'
in the case no telephone number is found. 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: -"
.
Can anybody advise how to handle this error?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…