我解析與AttributeError的:「NoneType」對象有沒有屬性「get_text」
Telephone = soup.find(itemprop="telephone").get_text()
HTML文本在電話號碼是itemprop
標籤後的HTML的情況下,我收到了許多,並得到輸出(例如,"Telephone Number: 34834243244"
)。
當然,我收到沒有電話號碼被發現的情況下AttributeError: 'NoneType' object has no attribute 'get_text'
。沒事兒。
但是,在這種情況下,我希望Python不要打印錯誤消息,而是設置Telephone = "-"
並獲得輸出"Telephone Number: -"
。
誰能告訴我如何處理這個問題?
兩種選擇,要麼:測試'是None'事先;或'嘗試'並捕獲'AttributeError'。 – jonrsharpe 2015-02-07 20:53:00