我有一些关于漂亮汤的 html 抓取代码问题。我不知道如何浏览整个 html 文档来找到我要查找的其余内容。
我有这段代码可以在下面的 html 中找到并打印单词“Totem”。我希望能够循环浏览 html 并找到剩余的“一、二、三”和“出租”
用于查找第一个标签和文本的代码:
print(html.find('td', {'class': 'play'}).next_sibling.next_sibling.text)
将以下内容作为要抓取的示例 html:
<tr>
<td class="play">
<a href="#" class="audio-preview"><span class="play-button as_audio-button"></span></a>
<audio class="as_audio_preview" src="https://shopify.audiosalad.com/" >foo</audio>
</td>
**<td>Totem</td>**
<!--<td>$0.99</td>-->
<td class="buy">
<tr>
<td class="play">
<a href="#" class="audio-preview"><span class="play-button as_audio-button"></span></a>
<audio class="as_audio_preview" src="https://shopify.audiosalad.com/" >foo</audio>
</td>
**<td>One, Two, Three</td>**
<!--<td>$0.99</td>-->
<td class="buy">
<tr>
<td class="play">
<a href="#" class="audio-preview"><span class="play-button as_audio-button"></span></a>
<audio class="as_audio_preview" src="https://shopify.audiosalad.com/" >foo</audio>
</td>
**<td>Rent</td>**
<!--<td>$0.99</td>-->
<td class="buy">
请您参考如下方法:
试试这个。它应该为您获取您想要的内容:
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,"lxml")
for items in soup.find_all(class_="play"):
data = items.find_next_sibling().text
print(data)
或者,您也可以这样尝试:
for items in soup.find_all(class_="play"):
data = items.find_next("td").text
print(data)
输出:
Totem
One, Two, Three
Rent
