gpt4 book ai didi

python - 使用 Python 自动从 ASPX 网站下载文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:39 26 4
gpt4 key购买 nike

这是我第一次在堆栈溢出上发帖,我期待更多地参与社区。我需要从 ASPX 网站下载、重命名和保存许多 Excel 文件,但无法直接通过 URL 访问这些 Excel 文件(即 URL 不以“excelfilename.csv”结尾)。我能做的就是访问启动文件下载的 URL。 URL 示例如下。

https://www.websitename.com/something/ASPXthing.aspx?ReportName=ExcelFileName&Date=SomeDate&reportformat=csv

我想通过循环改变的输入是“ExcelFileName”和“SomeDate”。我知道当可以通过 URL 直接访问 Excel 文件时,可以使用 urllib 获取这些文件,但是如何使用像这样的 URL 来做到这一点?

预先感谢您的帮助!

最佳答案

使用requests库,您可以获取文件并迭代 block 以写入文件

import requests

report_names = ["Filename1","Filename2"]
dates = ['2016-02-22','2016-02-23'] # as strings
for report_name in report_names:
for date in dates:
with open('%s_%s_fetched.csv' % (report_name.split('.')[0],date,), 'wb') as handle:
response = requests.get('https://www.websitename.com/something/ASPXthing.aspx?ReportName=%s&Date=%s&reportformat=csv' % (report_name,date,), stream=True)

if not response.ok:
# Something went wrong

for block in response.iter_content(1024):
handle.write(block)

关于python - 使用 Python 自动从 ASPX 网站下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35634724/

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