我是BeautifulSoup的新手,想用它来提取98.2%和94.2%的元素。我想打印:
**苹果:98.2%香蕉:94.2%
我该怎么做?先谢了。
<div>
<table class="stock">
<tr>
<th></th>
<th scope="col">Equal</th>
<th scope="col">Total</th>
<th scope="col">Fruits</th>
</tr>
<tr>
<th scope="row">apples:</th>
<td>524</td>
<td>525</td>
<td class="high">98.2%</td>
</tr>
<tr>
<th scope="row">pears:</th>
<td>58</td>
<td>58</td>
<td class="high">100.0%</td>
</tr>
<tr>
<th scope="row">bananas:</th>
<td>165</td>
<td>179</td>
<td class="high">94.2%</td>
</tr>
</table>
最初,我尝试了以下操作,但它会打印:【98.2%,100.0%,94.2%】
from bs4 import BeautifulSoup
HTMLFile = open("stock.html", "r")
index = HTMLFile.read()
soup = BeautifulSoup(index, 'html.parser')
element = soup.select(".stock .high")
print(element)
1条答案
按热度按时间ffx8fchx1#
试试看:
图纸: