Python 3.x requests.get(url).headers.get(';content disposition';)在PYTHON上不返回任何内容
嗯,在我的工作中,我需要自动化一个过程(实际上我是一名实习生),我只是想知道我是否可以使用Python来实现这样的过程。我仍在处理如何做这些事情的想法,现在我正在尝试了解如何使用python3从web URL下载文件。我在另一个网站上找到了一个指南,但那里没有主动帮助。我被告知使用模块Python 3.x requests.get(url).headers.get(';content disposition';)在PYTHON上不返回任何内容,python-3.x,url,python-requests,Python 3.x,Url,Python Requests,嗯,在我的工作中,我需要自动化一个过程(实际上我是一名实习生),我只是想知道我是否可以使用Python来实现这样的过程。我仍在处理如何做这些事情的想法,现在我正在尝试了解如何使用python3从web URL下载文件。我在另一个网站上找到了一个指南,但那里没有主动帮助。我被告知使用模块requests下载实际文件,使用模块re获取实际文件名 代码运行得很好,但后来我尝试添加一些功能,如GUI,但它只是停止了工作。我删除了GUI代码,但它不再工作了。现在我不知道该怎么做才能让代码正常工作,请有人帮
requests
下载实际文件,使用模块re
获取实际文件名
代码运行得很好,但后来我尝试添加一些功能,如GUI,但它只是停止了工作。我删除了GUI代码,但它不再工作了。现在我不知道该怎么做才能让代码正常工作,请有人帮我,谢谢:)
代码:
有什么错误吗?你确定标题是
内容处置
?@ewong是的,我确定标题是内容处置
。。我不知道我是否可以把网站放在这里,但你看:我刚刚把这些功能放到了一个新的功能中。没有错误,函数download
只返回NONE,因为它输入了第一个if语句。试图在浏览器中打开时显示无效url。@Tarique它在Chrome上对我有效,我已经测试过其他url。我不知道,但是命令请求。get(url)
返回一个响应400(错误请求),当我尝试使用github下载链接时,我得到403(禁止)。。。
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")```