我试图从以下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>
我这样试过:
soup = BeautifulSoup(html)
for hit in soup.findAll(attrs={'class' : 'MYCLASS'}):
print hit.text
但是我得到所有嵌套标记之间的所有文本加上注释。
有谁能帮我把“这是我的短信”从这里面拿出来吗?
请改用^{} :
是的,这有点像跳舞。
输出:
了解有关如何导航through the parse tree in ^{} 的详细信息。解析树得到了
tags
和NavigableStrings
(因为这是一个文本)。一个例子要向下移动解析树,有
contents
和string
。对于以上,也就是说你可以
对于多个子节点,可以有
因此,您可以在这里玩
contents
,并在所需索引处获取内容。您还可以在标记上迭代,这是一个快捷方式。例如
您可以使用^{} :
相关问题 更多 >
编程相关推荐