我正在刮美丽的汤的网站。我正在查找表中的“prtype”文本。我的问题是,这个专栏并不总是存在。python beautifulsoup .text无类型
如果列存在以下代码工作正常:
prtyp = soup.find("dd", attrs={"class":"is_type g"}).text.strip()
但是,如果没有与这个类,我得到以下错误无柱:
'NoneType' object has no attribute 'text'
那是我的尝试之一摆脱这个问题,但是prtyp是一个str,我得到整个html标签,或者.text不起作用。当然。
prtyp = soup.find("dd", attrs={"class":"is_type g"})
if prtyp is None:
prtyp = "no type"
else:
whgtyp.text.strip()
print("prtype:", prtype)
'尝试:whgtyp.text.strip() 打印( “prtype:” prtype); AttributeError除外:prtyp =“no type”' – davedwards