gpt4 book ai didi

Python - 使用 BeautifulSoup 抓取 ESPN 表

转载 作者:行者123 更新时间:2023-11-28 18:26:21 25 4
gpt4 key购买 nike

我正在尝试使用 BeautifulSoup 来抓取 this 上的“Season Stats”表页。有什么办法可以将整张 table 变成一个汤对象吗?目前我的代码是这样的:

seasonStats = soup.find('table', {'id': 'statsTable'})
categoryList = seasonStats.findAll('tr')[2].findAll('a')

我遇到的问题是 FG%、FT%、3PM、REB、AST、STL、BLK、TO、PTS 存储在一行中,但 RK、LAST、MOVES 存储在另一行中。无论如何我可以正确地抓取整个表,其中 RK、TEAM、FG%、FT%、3PM、REB、AST、STL、BLK、TO、PTS、LAST、MOVES 都存储在一行(categoryList)中? ESPN 甚至将这些值放在不同的行中,这似乎很愚蠢。此外,如果我能将整个表格整合到一个矩阵中,那将是非常有帮助的。

期望的输出:

['RK', 'TEAM', 'FG%', 'FT%', '3PM', 'REB', 'AST', 'STL', 'BLK', 'TO', 'PTS', 'LAST', 'MOVES']
['1', 'Team Li', '.4656', '.8049', '437', '1752', '962', '284', '228', '578', '4804', '4-4-1', '12']
['2', 'Team Aguilar', '.4499', '.7727', '415', '1925', '737', '276', '292', '543', '4901', '4-4-1', '0']
['3', 'Suck MyDirk', '.4533', '.8083', '410', '1798', '1035', '367', '153', '658', '5331', '3-6-0', '8']
['4', 'Knicks Tape', '.4589', '.8057', '339', '1458', '1029', '285', '132', '566', '4304', '4-5-0', '12']
['5', 'Kris Kaman His Pants', '.4576', '.8068', '534', '1530', '940', '306', '115', '515', '4603', '5-4-0', '17']
['6', 'Nutz Vs. Draymond Green', '.4518', '.8000', '404', '1641', '1004', '270', '176', '620', '4554', '5-4-0', '12']
['7', 'Team Keyrouze', '.4548', '.7895', '441', '1558', '809', '293', '195', '510', '4874', '4-5-0', '11']
['8', 'In Porzingod We Trust', '.4607', '.7542', '275', '1699', '1020', '274', '252', '482', '4119', '6-3-0', '13']
['9', 'Team Iannetta', '.4706', '.7908', '260', '1800', '1026', '310', '137', '646', '4909', '8-1-0', '13']
['10', "Jesse's Blue Balls", '.4646', '.6766', '403', '2029', '505', '243', '238', '481', '3929', '5-4-0', '16']
['11', 'Team Pauls 2 da Wall', '.4531', '.7602', '313', '1797', '1197', '313', '268', '525', '3719', '6-3-0', '13']
['12', 'YOU REACH, I TEACH', '.4552', '.7591', '401', '1488', '997', '285', '108', '521', '3694', '4-5-0', '12']
['13', 'Team Noey', '.4740', '.7610', '273', '1821', '681', '301', '226', '491', '4059', '3-6-0', '9']
['14', 'Team Jackson', '.4325', '.7484', '206', '1104', '714', '174', '101', '383', '2532', '1-8-0', '4']

当前输出:

['1', 'Team Li', '.4656', '.8049', '437', '1752', '962', '284', '228', '578', '4804', '4-4-1', '12']
['2', 'Team Aguilar', '.4499', '.7727', '415', '1925', '737', '276', '292', '543', '4901', '4-4-1', '0']
['3', 'Suck MyDirk', '.4533', '.8083', '410', '1798', '1035', '367', '153', '658', '5331', '3-6-0', '8']
['4', 'Knicks Tape', '.4589', '.8057', '339', '1458', '1029', '285', '132', '566', '4304', '4-5-0', '12']
['5', 'Kris Kaman His Pants', '.4576', '.8068', '534', '1530', '940', '306', '115', '515', '4603', '5-4-0', '17']
['6', 'Nutz Vs. Draymond Green', '.4518', '.8000', '404', '1641', '1004', '270', '176', '620', '4554', '5-4-0', '12']
['7', 'Team Keyrouze', '.4548', '.7895', '441', '1558', '809', '293', '195', '510', '4874', '4-5-0', '11']
['8', 'In Porzingod We Trust', '.4607', '.7542', '275', '1699', '1020', '274', '252', '482', '4119', '6-3-0', '13']
['9', 'Team Iannetta', '.4706', '.7908', '260', '1800', '1026', '310', '137', '646', '4909', '8-1-0', '13']
['10', "Jesse's Blue Balls", '.4646', '.6766', '403', '2029', '505', '243', '238', '481', '3929', '5-4-0', '16']
['11', 'Team Pauls 2 da Wall', '.4531', '.7602', '313', '1797', '1197', '313', '268', '525', '3719', '6-3-0', '13']
['12', 'YOU REACH, I TEACH', '.4552', '.7591', '401', '1488', '997', '285', '108', '521', '3694', '4-5-0', '12']
['13', 'Team Noey', '.4740', '.7610', '273', '1821', '681', '301', '226', '491', '4059', '3-6-0', '9']
['14', 'Team Jackson', '.4325', '.7484', '206', '1104', '714', '174', '101', '383', '2532', '1-8-0', '4']

非常感谢。

最佳答案

我想你错了。一个团队的所有数据看起来都在同一个 tr 中。这是第一个,删除了所有样式:

<tr>
<td id="sovrRk_9">1</td>
<td><a title="Team Li (Royce Li)" href="...">Team Li</a></td>
<td><spacer type="block" width="1" height="1"> </spacer>
</td>
<td id="tmTotalStat_9_19">.4656</td>
<td id="tmTotalStat_9_20">.8049</td>
<td id="tmTotalStat_9_17">437</td>
<td id="tmTotalStat_9_6">1752</td>
<td id="tmTotalStat_9_3">962</td>
<td id="tmTotalStat_9_2">284</td>
<td id="tmTotalStat_9_1">228</td>
<td id="tmTotalStat_9_11">578</td>
<td id="tmTotalStat_9_0">4804</td>
<td>4-4-1</td>
<td title="Season Moves">12</td>
</tr>

一切都在那里。

关于Python - 使用 BeautifulSoup 抓取 ESPN 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41254231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com