You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何爬取NBA首发阵容数据并生成Pandas DataFrame?

解决代码

import requests
from bs4 import BeautifulSoup
import pandas as pd

# 页面请求
url = "https://www.rotowire.com/basketball/nba-lineups.php"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")

data = []
# 定位所有球队的阵容块
lineup_boxes = soup.find_all(class_='lineup__box')

for box in lineup_boxes:
    # 提取球队缩写
    team_abbrev = box.find(class_='lineup__team-abbrev').text.strip()
    # 取前5名球员即为首发,自动过滤替补
    players = box.find_all(class_='lineup__player')[:5]
    for p in players:
        # 提取位置信息
        pos = p.find(class_='lineup__pos').text.strip()
        # 提取球员全名(a标签的title属性)
        player_name = p.find('a')['title'].strip()
        data.append({
            'Player': player_name,
            'Team': team_abbrev,
            'Position': pos
        })

# 转换为Pandas DataFrame
df = pd.DataFrame(data)

# 按球队分组打印,组间加空行匹配预期输出格式
for team, group in df.groupby('Team', sort=False):
    print(group.to_string(index=False))
    print('\n')

补充说明

  • 新增请求头避免触发网站反爬,提升请求成功率
  • 自动匹配球员对应的所属球队,不需要手动做队名映射
  • 如需将数据保存到本地,可追加代码df.to_csv('nba首发阵容.csv', index=False)或df.to_excel('nba首发阵容.xlsx', index=False)

内容的提问来源于stack exchange,提问作者Able Archer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 14:39:03