我正在做一个项目,我想搜集2019/20赛季从10月到8月的NBA比赛统计数据
我只关注主队和客场球队的比赛结果,而不是球员/球队的具体统计数据,因此我需要使用“基本方框分数统计”表获得每场比赛的方框分数数据
问题:在抓取禁区得分时,我只收集客队的数据,因为这是禁区得分链接中的第一个表,我只需使用索引[0]指定该表(它是静态的)。对于主队来说,表索引似乎会根据是否有随时间变化(OT)而变化,有时还会由于其他未指定的变化(这有点动态)
问题:如何最好地使用循环来收集客场和主队每个月的方块分数?或者,我如何为主队收集每个框得分的数据
一段时间内不带的比赛的方框得分页面示例: https://www.basketball-reference.com/boxscores/201910220LAC.html
随着时间的推移,与进行比赛的框得分页面示例: https://www.basketball-reference.com/boxscores/201910220TOR.html
在后一个示例中,主队的表索引根据前面的表数(包含数据的表,如随时间变化等)而变化。通常是第八张没有加班的桌子,而有加班的桌子则不同
我成功(且一致)获取客场球队数据的代码如下:
box_score_example_url='http://www.basketball-reference.com//boxscores/201910230POR.html'
dfbox[]
for eachBox in box_score_example_url:
dfz = pd.read_html(eachBox)
dfbox.append(dfz[0])
boxbox_awayteam = pd.concat(dfbox)
boxbox_awayteam
我没有这个想法,因为在HTML代码中似乎没有任何表具有特定的id或类。这是我的第一个网页抓取项目,也是我在Stackoverflow上提出的第一个问题,我对此一无所知
您可以使用BeautifulSoup和CSS选择器
[id$="-game-basic"] table
仅选择两个基本表,然后使用pd.read_html()
加载这些表:印刷品:
编辑:要将此函数放入循环中,可以使用以下示例:
印刷品:
相关问题 更多 >
编程相关推荐