Python BeautifulSoup 从 html 文件中提取元素

问题描述 投票:0回答:1

我是 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)
html python-3.x beautifulsoup extract
1个回答
0
投票

尝试:

from bs4 import BeautifulSoup

html_text = """\
  <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>"""

soup = BeautifulSoup(html_text, "html.parser")

for tr in soup.select('tr:-soup-contains("apples", "bananas")'):
    print(tr.th.text, tr.find(class_="high").text)

打印:

apples: 98.2%
bananas: 94.2%
© www.soinside.com 2019 - 2024. All rights reserved.