使用Python从HTML表中只提取一列数据?

2024-06-14 09:18:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图为我正在做的一个小项目提取一些NBA统计数据,我需要从HTML表中提取一些列(垂直向上和向下)数据,比如this one here。我现在只想得到PTS,那么我该如何只提取那一列数据呢?我发现它是每个数据行中倒数第三个元素,但我不确定应该如何解析数据。在


Tags: 数据项目元素herehtmlthisonepts
1条回答
网友
1楼 · 发布于 2024-06-14 09:18:09

我建议你阅读整个表格,然后我建议你阅读整个表格。也许你会在速度上失去一些东西,但在简单中你会得到更多。在

使用pandas的read_html函数很容易做到:

import urllib2
import pandas as pd

page1 = urllib2.urlopen(
    'http://www.basketball-reference.com/players/h/hardeja01/gamelog/2015/').read()

#Select the correct table by some attributes, in this case id=pgl_basic.
#The read_html function returns a list of tables.
#In this case we select the first (and only) table with this id
stat_table = pd.io.html.read_html(page1,attrs={'id':'pgl_basic'})[0]

#Just select the column we needed. 
point_column = stat_table['PTS']

print point_column

如果您还不熟悉熊猫,您可以从以下内容中了解更多: http://pandas-docs.github.io/pandas-docs-travis/10min.html

例如,您可能希望从表中删除标题行或将表拆分为多个表。在

相关问题 更多 >