【问题标题】:Python Web Scraping Html Table using beautiful soupPython Web Scraping Html Table 使用漂亮的汤
【发布时间】:2018-09-05 07:27:06
【问题描述】:

这是我的 HTML 表格。

<table class="table_c" id="myd">
<tbody>
    <tr class="grp">
        <th class="col>MyGrp1</th>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item0.1 Header</th>
        <td class="col data" data-th="MyGrp1">Item0.1 Value</td>
    </tr>
    <tr class="grp">
        <th class="col label" colspan="2" scope="row">MyGrp</th>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item1.1 Header</th>
        <td class="col data" >Item1.1 Value</td>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item1.2 Header</th>
        <td class="col data">Item1.2 Value</td>
    </tr>
    <tr class="item">
    <th class="col label" scope="row">Item1.3 Header</th>
    <td class="col data"">Item1.2 Value</td>
    </tr>
</tbody>
</table>

我希望表格解析如下

MyGrp1<new line>
<tab char>Item0.1 Header<tab char>Item0.1 Value<new line>
MyGrp2<new line>
<tab char>Item1.1 Header<tab char>Item1.1 Value<new line>
<tab char>Item1.2 Header<tab char>Item1.2 Value<new line>
<tab char>Item1.3 Header<tab char>Item1.3 Value<new line>

我可以得到'tr'或'th'的所有节点。但我不知道如何逐个节点迭代表节点。如何抓取 Html 表并获得上述结果?

【问题讨论】:

  • 请向我们展示您的代码,并告诉我们您到目前为止所做的尝试。你用的是什么 html 解析器?
  • 从 bs4 导入请求 import BeautifulSoup table_t = soup.find('table', class_='table_c') for tr in table_t.findAll('tr',class_='grp'):跨度>

标签: python html-table beautifulsoup


【解决方案1】:

我为此使用了熊猫

import pandas as pd
import html5lib

string="""<table class="table_c" id="myd">
<tbody>
    <tr class="grp">
        <th class="col">MyGrp1</th>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item0.1 Header</th>
        <td class="col data" data-th="MyGrp1">Item0.1 Value</td>
    </tr>
    <tr class="grp">
        <th class="col label" colspan="2" scope="row">MyGrp</th>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item1.1 Header</th>
        <td class="col data" >Item1.1 Value</td>
    </tr>
    <tr class="item">
        <th class="col label" scope="row">Item1.2 Header</th>
        <td class="col data">Item1.2 Value</td>
    </tr>
    <tr class="item">
    <th class="col label" scope="row">Item1.3 Header</th>
    <td class="col data"">Item1.2 Value</td>
    </tr>
</tbody>
</table>"""
df = pd.read_html(string)
print(df)

输出

[                0              1
0          MyGrp1            NaN
1  Item0.1 Header  Item0.1 Value
2           MyGrp            NaN
3  Item1.1 Header  Item1.1 Value
4  Item1.2 Header  Item1.2 Value
5  Item1.3 Header  Item1.2 Value]

【讨论】:

    【解决方案2】:

    我做了以下得到答案。我在这里给出我的解决方案。如果我错了,请纠正我。

    result = ""
    for tr in table_t.findAll('tr'):
        if 'grp' in tr.get("class"):
            for th in tr.findAll('th'):
                result += "\n" + th.text.strip()
                #print(th.text.strip())
        elif 'item' in tr.get("class"):
            children_th = tr.find("th")
            children_td = tr.find("td")
            result += "\n\t" + children_th.text.strip() + "\t" + children_td.text.strip()
    print(result)
    

    【讨论】:

      【解决方案3】:

      但我不知道如何逐个节点迭代表。

      BeautifulSoupfind_all 为您提供了一系列可以循环的标记对象。

      另外请注意,您的 html 表存在语法问题: &lt;th class="col&gt;MyGrp1&lt;/th&gt; - 缺少报价 &lt;td class="col data""&gt;Item1.2 Value&lt;/td&gt; - 双引号

      因此,如果 sample 是您的 html 表格,并且它具有有效的 html,那么您可以执行以下操作:

      from bs4 import BeautifulSoup as bs
      
      soup = bs(sample, 'lxml-html')
      trs = soup.find_all('tr')
      group = None # in case there are items before the first group
      for tr in trs:
          if 'grp' in tr.get('class'):
              print(tr.th.text)
          elif 'item' in tr.get('class'):
              label = tr.th.text
              value = tr.td.text
              print('{} {}'.format(label, value))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 2013-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多