首页 > 解决方案 > wget.download 在尝试下载图像时引发 indexerror

问题描述

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

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

    wget.download(url, f"C:/Users/Family/Pictures/downloads/{name}")
  File "C:\Users\Family\AppData\Local\Programs\Python\Python38\lib\site-packages\wget.py", line 527, in download
    filename = detect_filename(url, out, headers)
  File "C:\Users\Family\AppData\Local\Programs\Python\Python38\lib\site-packages\wget.py", line 486, in detect_filename
    names["headers"] = filename_from_headers(headers) or ''
  File "C:\Users\Family\AppData\Local\Programs\Python\Python38\lib\site-packages\wget.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}")

标签: pythonpython-3.xfilewgetindex-error

解决方案


推荐阅读