【问题标题】:Get the table(only values) from two different tables?从两个不同的表中获取表(仅值)?
【发布时间】:2019-12-05 13:03:09
【问题描述】:

我想从两个具有相同类的不同表中获取或选择数据。我尝试从“soup.find_all”中获取数据,但格式化数据变得越来越困难。 有两个相同类的表。我只需要从表中获取值(而不是标签)。

表 1:

<div class="bh_collapsible-body" style="display: none;">
  <table border="0" cellpadding="2" cellspacing="2" class="prop-list">
    <tbody>
    <tr>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Rim Material</td>
            <td class="value">Alloy</td>
          </tr>
          </tbody>
        </table>
      </td>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Front Tyre Description</td>
            <td class="value">215/55 R16</td>
          </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Front Rim Description</td>
            <td class="value">16x7.0</td>
          </tr>
          </tbody>
        </table>
      </td>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Rear Tyre Description</td>
            <td class="value">215/55 R16</td>
          </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Rear Rim Description</td>
            <td class="value">16x7.0</td>
          </tr>
          </tbody>
        </table>
      </td>
      <td></td>
    </tr>
    </tbody>
  </table>
</div>
</div>

表 2:

<div class="bh_collapsible-body" style="display: none;">
  <table border="0" cellpadding="2" cellspacing="2" class="prop-list">
    <tbody>
    <tr>
      <td class="item">
        <table>
          <tbody>
          <tr>
            <td class="label">Steering</td>
            <td class="value">Rack and Pinion</td>
          </tr>
          </tbody>
        </table>
      </td>
      <td></td>
    </tr>
    </tbody>
  </table>
</div>
</div>

我尝试过的:

我尝试从 Xpath 获取第一个表格内容,但它提供了值和标签。

table1 = driver.find_element_by_xpath("//*[@id='features']/div/div[5]/div[2]/div[1]/div[1]/div/div[2]/table/tbody/tr[1]/td[1]/table/tbody/tr/td[2]")

我尝试拆分数据但没有成功

【问题讨论】:

  • 尝试xpath:driver.find_element_by_xpath('//*[@id='features']//table//td[@class="value"]').text

标签: python selenium xpath beautifulsoup


【解决方案1】:

我想你正在寻找 CSS 选择器tr:not(:has(tr)),这将选择最里面的&lt;tr&gt;

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser') # the variable data contains string for Table1 and Table2 in your question

rows = []
for tr in soup.select('tr:not(:has(tr))'):
    rows.append([td.get_text(strip=True) for td in tr.select('td')])

for row in zip(*rows):
    print(''.join('{: ^25}'.format(d) for d in row))

打印:

  Rim Material        Front Tyre Description    Front Rim Description    Rear Tyre Description    Rear Rim Description           Steering         
      Alloy                 215/55 R16                 16x7.0                 215/55 R16                 16x7.0               Rack and Pinion     

变量rows 包含:

[['Rim Material', 'Alloy'],
 ['Front Tyre Description', '215/55 R16'],
 ['Front Rim Description', '16x7.0'],
 ['Rear Tyre Description', '215/55 R16'],
 ['Rear Rim Description', '16x7.0'],
 ['Steering', 'Rack and Pinion']]

进一步阅读:

CSS Selectors Reference

编辑:将 CSS 选择器更改为 tr:not(:has(tr))

【讨论】:

  • 我得到了这个 NotImplementedError:只实现了以下伪类:nth-​​of-type。
  • @thoris 确保您拥有最新版本的 BeautifulSoup。我在版本beautifulsoup4==4.8.0
  • @thoris 这是不可能的。输入pip freeze 以查看实际版本。另外,您是从虚拟环境还是 Jupyter 笔记本运行 Python?这可能是您版本不匹配的原因。
  • 我从 jupyter 运行它,在终端(v env)和 jupyter 版本相同的 4.8.0
  • @thoris 运行此代码import bs4; print(bs4.__version__),这将显示您在脚本中运行的版本。 soupsieve 依赖项也应该在版本 soupsieve==1.9.2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2017-07-28
  • 2021-06-21
  • 1970-01-01
相关资源
最近更新 更多