Python BeautifulSoup从html文件中提取元素

5anewei6  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(165)

我是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)
ffx8fchx

ffx8fchx1#

试试看:

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%

相关问题