2015-04-05 76 views
0

我正在尝试为我正在做的一个小项目提取一些NBA统计信息,并且我需要从HTML表格中提取几列(垂直向上和向下)数据,如this one here。我现在只想获得PTS,所以我应该如何才能提取那一列数据?我发现它是每个数据行的倒数第三个元素,但我不知道应该如何解析数据。从HTML表格提取一列数据w/Python?

回答

1

我建议你阅读整个html表格,然后选择你需要的列。也许你会在速度上失去一些东西,但你会在简单中获得更多。

这是很容易做到与大熊猫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

例如,您可能要删除表格中的标题行或将表格拆分为多个表格。