相关疑难解决方法(0)

Python BeautifulSoup提取元素之间的文本

我尝试从以下HTML中提取"这是我的文本":

<html>
<body>
<table>
   <td class="MYCLASS">
      <!-- a comment -->
      <a hef="xy">Text</a>
      <p>something</p>
      THIS IS MY TEXT
      <p>something else</p>
      </br>
   </td>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我这样试过:

soup = BeautifulSoup(html)

for hit in soup.findAll(attrs={'class' : 'MYCLASS'}):
    print hit.text
Run Code Online (Sandbox Code Playgroud)

但我得到所有嵌套标签和评论之间的所有文本.

任何人都可以帮助我从中获得"这是我的文字"吗?

python beautifulsoup

33
推荐指数
5
解决办法
14万
查看次数

标签 统计

beautifulsoup ×1

python ×1