提问者:小点点

Python:Pandas使用它唯一的表标题读取特定的html表


我要开始研究蟒蛇熊猫,需要一些指导。 假设我有一个由多个表组成的html文件,并且每个表由每个表的标题(例如表号135)唯一标识。

如果我希望利用唯一的标题“table 246”来识别和读取那个表,那么Python熊猫如何能够专门读取这个表而忽略其他表呢? 我需要阅读这个表246,使用它的标题,因为,这个文件中的表的顺序不是固定的,它是动态的。

我一直在网上搜索,但没有找到任何关于使用表的标题来识别该表的解决方案。

我的html文件内容

<html>
<head>
  <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
</head>
<body>
<p>
<table border=1>
<caption align=left>Table 135</caption>
<tr bgcolor="#d4d0c8" bordercolor=black>
<th bordercolor=black>User Name</th>
<th bordercolor=black>Mobile Number</th>
</tr>
<tr bordercolor=black>
<td bordercolor=black bgcolor=white>John</td>
<td bordercolor=black bgcolor=white>1234567890</td>
</tr>
</table>
</p>
<p>
<table border=1>
<caption align=left>Table 246</caption>
<tr bgcolor="#d4d0c8" bordercolor=black>
<th bordercolor=black>Salary</th>
<th bordercolor=black>Experience</th>
</tr>
<tr bordercolor=black>
<td bordercolor=black bgcolor=white>$5000</td>
<td bordercolor=black bgcolor=white>10</td>
</tr>
</table>
</p>
</body>
</html>

共1个答案

匿名用户

您可以尝试以下代码来提取html表:

import pandas as pd

df = pd.read_html("test.html", match='Table 246')
df[0]

示例代码:

https://github.com/biranchi2018/misc/blob/master/1.正在使用%20pandas.ipynb提取%20html%20

谢谢

相关问题