使用BeautifulSoup迭代HTML

2024-05-16 13:11:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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

这个代码不起作用,有人能帮忙吗。。。?你知道吗


Tags: 文件name标记内容dochtmltagtags