wget.download在尝试下载图像时会引发索引错误



我最近一直在尝试wget,而不是请求,它非常简单易用,但我在特定链接方面遇到了问题。

当我出于某种原因尝试从wikipedia下载png图像时,wget.download在尝试写入文件时不断引发IndexError此错误具体为:

wget.download(url, f"C:/Users/Family/Pictures/downloads/{name}")
File "C:UsersFamilyAppDataLocalProgramsPythonPython38libsite-packageswget.py", line 527, in download
filename = detect_filename(url, out, headers)
File "C:UsersFamilyAppDataLocalProgramsPythonPython38libsite-packageswget.py", line 486, in detect_filename
names["headers"] = filename_from_headers(headers) or ''
File "C:UsersFamilyAppDataLocalProgramsPythonPython38libsite-packageswget.py", line 258, in filename_from_headers
name = fnames[0].split('=')[1].strip(' t"')
IndexError: list index out of range

我试着设置一个特定的文件名,但仍然没有成功。当我在cmd中使用wget时,url似乎没有问题,那么我该如何解决这个问题呢?

import wget

# This is the link to the image
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png"
# It does not seem to have a problem detecting the filename
name = wget.detect_filename(url)
# I tried to set a specific filename but I still got the error with ot whout it
wget.download(url, f"C:/Users/Family/Pictures/downloads/{name}")

我有和您相同的情况。当我对回复标头进行一些研究时,在我的情况下,如果URL标头不包含"内容处置":"附件;filename=";xxx">,它会抛出这个错误,希望这能有所帮助。

最新更新