BeautifulSoup を使用して HTML ファイルを反復処理し、「Preferred Name」というコンテンツを含むタグを見つけたいと考えています。探しているタグは次のとおりです:(これは、検索したいファイルの一部です):
<td nowrap class="label">
Preferred Name
<span class="slot_labels"></span>
</td>
これで検索しようとしました(docはそのhtmlファイルの名前です):
soup = BeautifulSoup(doc)
tags = soup.fetch('td')
for tag in tags:
if tag.contents[0] == 'Preferred Name':
return tag
このコードは機能しません。誰か助けてくれますか?