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

如何将requests从nhl.com爬取的表格数据导出到Excel工作簿/工作表

步骤1:安装必要依赖

要实现导出Excel功能,需要先安装pandas和openpyxl(Excel读写引擎),执行以下命令:

pip install pandas openpyxl

步骤2:修改代码完成导出

你拿到的data['data']本身就是标准的字典列表结构,pandas可以直接将其转换为DataFrame格式,不需要额外复杂处理,完整代码如下:

import requests
import pandas as pd

url = 'https://api.nhle.com/stats/rest/en/team/powerplay?isAggregate=false&isGame=false&sort=%5B%7B%22property%22:%22powerPlayPct%22,%22direction%22:%22DESC%22%7D,%7B%22property%22:%22teamId%22,%22direction%22:%22ASC%22%7D%5D&start=0&limit=50&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20homeRoad=%22H%22%20and%20seasonId%3C=20212022%20and%20seasonId%3E=20212022'

data = requests.get(url).json()

# 直接将接口返回的结构化数据转换为DataFrame
df = pd.DataFrame(data['data'])

# 如果只需要保留球队全称、多打少成功率两个字段,可以加这行筛选
df = df[['teamFullName', 'powerPlayPct']]
# 将成功率转换为百分比格式
df['powerPlayPct'] = df['powerPlayPct'].apply(lambda x: round(x*100, 1))

# 导出到Excel,index=False表示不导出行索引
df.to_excel('nhl球队多打少成功率统计.xlsx', index=False)

说明

  • 接口返回的JSON数据本身就是结构化的,不需要额外解析HTML表格,直接传入pd.DataFrame()即可完成转换
  • 如果你需要保留接口返回的所有字段,删除筛选列的代码行即可
  • 导出的Excel文件会保存在代码运行的同级目录下

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 12:09:00