2014-10-22 145 views
1

我想凑这个页面:http://stats.nba.com/playerGameLogs.html?PlayerID=2544&pageNo=1&rowsPerPage=100刮网站使用JavaScript

我想获得该表为大熊猫数据帧。我试过BeautifulSoup,很明显这是行不通的。我试图使用硒,但我没有运气。我希望有人有更好的解决方案,然后继续下去硒路径,因为它至少打开浏览器并显示正确的输出,Firefox只是在关闭之后关闭。我也更喜欢不必物理地打开浏览器,因为我会这样做1000页的页面。

回答

3

没有必要刮掉HTML,或者使用高级别的selenium方法。

模拟去往服务器的底层XHR请求,并返回用于填充页面上表的JSON数据。

下面是一个使用requests一个例子:

import requests 

url = 'http://stats.nba.com/stats/playergamelog' 

params = { 
    'Season': '2013-14', 
    'SeasonType': 'Regular Season', 
    'LeagueID': '00', 
    'PlayerID': '2544', 
    'pageNo': '1', 
    'rowsPerPage': '100' 
} 
response = requests.post(url, data=params) 

print response.json() 

打印的JSON结构包含玩家的游戏日志:

{u'parameters': {u'LeagueID': u'00', 
       u'PlayerID': 2544, 
       u'Season': u'2013-14', 
       u'SeasonType': u'Regular Season'}, 
u'resource': u'playergamelog', 
u'resultSets': [{u'headers': [u'SEASON_ID', 
           u'Player_ID', 
           u'Game_ID', 
           u'GAME_DATE', 
           u'MATCHUP', 
           u'WL', 
           u'MIN', 
           u'FGM', 
           u'FGA', 
           u'FG_PCT', 
           u'FG3M', 
           u'FG3A', 
           u'FG3_PCT', 
           u'FTM', 
           u'FTA', 
           u'FT_PCT', 
           u'OREB', 
           u'DREB', 
           u'REB', 
           u'AST', 
           u'STL', 
           u'BLK', 
           u'TOV', 
           u'PF', 
           u'PTS', 
           u'PLUS_MINUS', 
           u'VIDEO_AVAILABLE'], 
        u'name': u'PlayerGameLog', 
        u'rowSet': [[u'22013', 
           2544, 
           u'0021301192', 
           u'APR 12, 2014', 
           u'MIA @ ATL', 
           u'L', 
           37, 
           10, 
           22, 
           0.455, 
           3, 
           7, 
           0.429, 
           4, 
           8, 
           0.5, 
           3, 
           5, 
           8, 
           5, 
           0, 
           1, 
           3, 
           2, 
           27, 
           -13, 
           1], 
           [u'22013', 
           2544, 
           u'0021301180', 
           u'APR 11, 2014', 
           u'MIA vs. IND', 
           u'W', 
           35, 
           11, 
           20, 
           0.55, 
           2, 
           4, 
           0.5, 
           12, 
           13, 
           0.923, 
           1, 
           5, 
           6, 
           1, 
           1, 
           1, 
           2, 
           1, 
           36, 
           13, 
           1], 
           [u'22013', 
           2544, 
           u'0021301167', 
           u'APR 09, 2014', 
           u'MIA @ MEM', 
           u'L', 
           41, 
           14, 
           23, 
           0.609, 
           3, 
           5, 
           0.6, 
           6, 
           7, 
           0.857, 
           1, 
           5, 
           6, 
           5, 
           2, 
           0, 
           5, 
           1, 
           37, 
           -8, 
           1], 
    ... 
} 

另一种解决方案是使用NBA的API,看到几个选项这里:

+0

令人惊叹。非常感谢 – user1610719 2014-10-22 02:25:10