好吧,我需要在我的工作中自动化一个流程(实际上我是一名实习生),我只是想知道我是否可以使用 Python 来完成这样的流程。我仍在处理如何做这些事情的想法,现在我正在尝试了解如何使用 python3 从 Web URL 下载文件。我在另一个网站上找到了指南,但那里没有积极的帮助。我被告知使用模块requests下载实际文件,并使用模块re获取真实文件名。
代码运行良好,但后来我尝试添加一些功能,如 GUI,但它就停止了工作。我取下了 GUI 代码,但它不再工作了。现在我不知道该怎么做才能使代码正常工作,请有人帮我,谢谢:)
代码:
import os
import re
# i have no idea of how this function works, but it gets the real file name
def getFilename(cd):
if not cd:
print("check 1")
return None
fname = re.findall('filename=(.+)', cd)
if len(fname) == 0:
print("check 2")
return None
return fname[0]
def download(url):
# get request
response = requests.get(url)
# get the real file name, cut off the quota and take the second element of the list(actual file name)
filename = getFilename(response.headers.get('content-disposition'))
print(filename)
# open in binary mode and write to file
#open(filename, "wb").write(response.content)
download("https://pixabay.com/get/57e9d14b4957a414f6da8c7dda353678153fd9e75b50704b_1280.png?attachment=")
os.system("pause")```