如何使用BeautifulSoup从一段html代码中获取价值?

时间:2019-03-09 04:23:40

标签: python html beautifulsoup

我刚刚开始使用python进行某些网页抓取,并且似乎推荐在任何地方使用 BeautifulSoup

我的内容如下:

<table class="table with-row-highlight table-archive">
  <tbody>
    <tr>
      <td>
        <div class="user-tagline ">
          <span class="username " data-avatar="aaaaaaa">player1</span>
          <span class="user-rating">(1357)</span>
          <span class="country-flag-small flag-113" tip="Portugal"></span>
        </div>
        <div class="user-tagline ">
          <span class="username " data-avatar="bbbbbbb">player2</span>
          <span class="user-rating">(1387)</span>
          <span class="country-flag-small flag-70" tip="Indonesia"></span>
        </div>
      </td>
      <td>
        <a class="clickable-link text-middle" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">
          <div class="pull-left">
            <span class="game-result">1</span>
            <span class="game-result">0</span>
          </div>
          <div class="result">
            <i class="icon-square-minus loss" tip="Lost"></i>
          </div>
        </a>
      </td>
      <td class="text-center">
        <a class="clickable-link" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">30 min</a>
      </td>
      <td class="text-right">
        <a class="clickable-link text-middle moves" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">25</a>
      </td>
      <td class="text-right miniboard">
        <a class="clickable-link archive-date" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">Aug 9, 2017</a>
      </td>
    </tr>
    100 <tr></tr> here
  </tbody>
</table>

我的代码到此为止,如何编写python代码来循环所有<tr></tr>对并提取每个<span>对中的每个<td>对的所有类?

编辑

我认为也许我在这里没有清楚解释,您的代码返回的是该HTML中的类的名称,而我正在寻找的是对应的值,例如有一个类username,我想获取其值player1player2;我要获得country-flag-small的课程flag-70 tip=Indonesia

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

import requests
from bs4 import BeautifulSoup

res = requests.get('someLink')
soup = BeautifulSoup(res.text)


classes = []
for element in soup.find_all(class_=True):
    classes.extend(element["class"])
print(classes)

我使用您的html文件对此进行了测试,并获得了以下结果:

['table', 'with-row-highlight', 'table-archive', 'user-tagline', 'username', 'user-rating', 'country-flag-small', 'flag-113', 'user-tagline', 'username', 'user-rating', 'country-flag-small','flag-70', 'clickable-link', 'text-middle', 'pull-left', 'game-result', 'game-result', 'result', 'icon-square-minus', 'loss', 'text-center', 'clickable-link', 'text-right', 'clickable-link', 'text-middle', 'moves', 'text-right', 'miniboard', 'clickable-link', 'archive-date']

请注意,如果您还没有pip3 install requests,则必须

此外,如果您要使用计算机上的文件对其进行测试,则可以执行以下操作:

from bs4 import BeautifulSoup

file = open('/path/To/Your/HtmlFile.html', 'r')
lines = file.read()
soup = BeautifulSoup(lines)


classes = []
for element in soup.find_all(class_=True):
    classes.extend(element["class"])
print(classes)