【发布时间】: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