0

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

このコードは機能しません。誰か助けてくれますか?

4

1 に答える 1

0

コンテンツには空白が含まれているので、これを試してください。

soup = BeautifulSoup(doc)
tags = soup.fetch('td')
for tag in tags:
    if tag.contents[0] and tag.contents[0].strip() == 'Preferred Name':
        return tag
于 2013-03-01T00:47:35.783 に答える