栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从HTML表中提取数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从HTML表中提取数据

使用BeautifulSoup4的Python解决方案(
编辑: 使用适当的跳过。 编辑3: 使用

选择
table
):

from bs4 import BeautifulSouphtml = """  <table  border="0" cellpadding="5" cellspacing="2" width="95%">    <tr valign="top">      <th>Tests</th>      <th>Failures</th>      <th>Success Rate</th>      <th>Average Time</th>      <th>Min Time</th>      <th>Max Time</th>   </tr>   <tr valign="top" >     <td>103</td>     <td>24</td>     <td>76.70%</td>     <td>71 ms</td>     <td>0 ms</td>     <td>829 ms</td>  </tr></table>"""soup = BeautifulSoup(html)table = soup.find("table", attrs={"class":"details"})# The first tr contains the field names.headings = [th.get_text() for th in table.find("tr").find_all("th")]datasets = []for row in table.find_all("tr")[1:]:    dataset = zip(headings, (td.get_text() for td in row.find_all("td")))    datasets.append(dataset)print datasets

结果看起来像这样:

[[(u'Tests', u'103'),  (u'Failures', u'24'),  (u'Success Rate', u'76.70%'),  (u'Average Time', u'71 ms'),  (u'Min Time', u'0 ms'),  (u'Max Time', u'829 ms')]]

Edit2: 要产生所需的输出,请使用类似以下的内容:

for dataset in datasets:    for field in dataset:        print "{0:<16}: {1}".format(field[0], field[1])

结果:

Tests: 103Failures        : 24Success Rate    : 76.70%Average Time    : 71 msMin Time        : 0 msMax Time        : 829 ms


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号