Page

在上面的页面上,如果您单击"下载CSV",它会将CSV文件下载到您的计算机.我想设置一个夜间流程来下载该CSV.我也很高兴抓取数据,CSV似乎更容易.我真的没有找到任何东西.帮助?

推荐答案

import requests

def get_daily_stats(url):
    response = requests.get(url, headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
        'Referer': 'https://baseballsavant.mlb.com/leaderboard/custom?year=2024&type=batter&filter=&min=q&selections=pa%2Ck_percent%2Cbb_percent%2Cwoba%2Cxwoba%2Csweet_spot_percent%2Cbarrel_batted_rate%2Chard_hit_percent%2Cavg_best_speed%2Cavg_hyper_speed%2Cwhiff_percent%2Cswing_percent&chart=false&x=pa&y=pa&r=no&chartType=beeswarm&sort=xwoba&sortDir=desc'
    })
    with open('daily_stats.csv', 'wb') as f:
        f.write(response.content)
    return

def main():
    url = 'https://baseballsavant.mlb.com/leaderboard/custom?year=2024&type=batter&filter=&min=q&selections=pa%2Ck_percent%2Cbb_percent%2Cwoba%2Cxwoba%2Csweet_spot_percent%2Cbarrel_batted_rate%2Chard_hit_percent%2Cavg_best_speed%2Cavg_hyper_speed%2Cwhiff_percent%2Cswing_percent&chart=false&x=pa&y=pa&r=no&chartType=beeswarm&sort=xwoba&sortDir=desc&csv=true'
    get_daily_stats(url)

if __name__ == '__main__':
    main()

这将为您下载CSV并将其保存到脚本所在的文件夹中的daily_stats.csv. 您还必须安装requests - python -m pip install requests. 如何每晚做到这一点更多的是什么最适合你的问题. 我的意思是,您可以每天晚上运行它,或者您的目标是在计算机上拥有一个自动运行它的进程?

我想这将在2025年停止工作,但您可以在此时更改URL中的年份.

Python相关问答推荐

如何在Python中使用ijson解析SON期间检索文件位置?

无法使用python.h文件; Python嵌入错误

symy.分段使用numpy数组

pandas DataFrame GroupBy.diff函数的意外输出

@Property方法上的inspect.getmembers出现意外行为,引发异常

点到面的Y距离

如何让这个星型模式在Python中只使用一个for循环?

pandas:排序多级列

Django—cte给出:QuerySet对象没有属性with_cte''''

try 检索blob名称列表时出现错误填充错误""

使用Python从rotowire中抓取MLB每日阵容

Polars将相同的自定义函数应用于组中的多个列,

以逻辑方式获取自己的pyproject.toml依赖项

Flask Jinja2如果语句总是计算为false&

python—telegraph—bot send_voice发送空文件

从列表中获取n个元素,其中list [i][0]== value''

python sklearn ValueError:使用序列设置数组元素

以异步方式填充Pandas 数据帧

使用polars. pivot()旋转一个框架(类似于R中的pivot_longer)

上传文件并使用Panda打开时的Flask 问题