0

嘿,我目前正在尝试解析一个网站,我几乎完成了,但是有一个小问题。我想从 html 代码中排除内部标签

<span class="moto-color5_5">
  <strong>Text 1 </strong>
  <span style="font-size:8px;">Text 2</span>
</span>

我尝试使用 ...find("span", "moto-color5_5")但这会返回

Text 1 Text 2 而不是只返回 Text 1

有什么建议么?

真诚的:)

4

1 回答 1

0

排除内部标签也会排除Text 1,因为它在内部标签<strong>中。

但是,您可以strong在当前汤中找到:

html = """<span class="moto-color5_5">
  <strong>Text 1 </strong>
  <span style="font-size:8px;">Text 2</span>
</span>
"""
soup = BeautifulSoup(html)
result = soup.find("span", "moto-color5_5").find('strong')
print(result.text) # Text 1
于 2019-10-05T18:05:12.500 回答