我正在关注bs4的一些教程。我正在尝试使用'a'来获取以下示例的get_text()。教程返回结果McDermott International和MDR没有问题。但是当我这样做时,我得到了AttributeError:'NoneType'对象没有属性'get_text'。请帮忙。非常感谢!
with open('Energy.htm') as f:
soup = BeautifulSoup(f,"lxml")
energylist = soup.find_all('td', {"style" : "text-align:left;"})
for stock in energylist:
try:
stock_name = stock.find('a').get_text()
except:
stock_name = ''
#sample of the energylist
[<td style="text-align:left;">
<a href="/finance?q=NYSE:MDR&ei=nblKWaDrOs7AmgH0l7S4Bg">McDermott
International</a>
</td>, <td style="text-align:left;">
<a href="/finance?q=NYSE:MDR&ei=nblKWaDrOs7AmgH0l7S4Bg">MDR</a>
</td>, <td style="text-align:left;">
<a href="/finance?q=NYSE:EQT&ei=nblKWaDrOs7AmgH0l7S4Bg">EQT</a>
</td>, <td colspan="8" style="text-align:left;">
Companies <b>1 - 20</b> of about <b>476</b> in <b>Energy</b>
</td>]
答案 0 :(得分:1)
看起来energylist
有一些标签不包含其中的锚标签。您需要添加一个条件来优雅地处理这些条件:
for stock in energylist:
try:
stock_name = stock.find('a').get_text()
... # more code
except AttributeError:
pass