python - 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}")
解决方案
推荐阅读
- django - 无法创建 Django CreateView 对象,因为数据未验证
- swift - 为什么 UITableView 为每个单元格显示相同的视图?
- reactjs - Jest Unit Test - 使用道具模拟组件
- android - Xamarin.Forms android WebView 空白,没有有效的 SSL 证书(开发环境)
- r - R查找按访问ID细分的总计数
- vb.net - 数据表到 Crystal Reports(缺少字段)
- javascript - 如何在不同的时间触发不同的 setInterval 函数?
- python - 使用 openpyxl 删除现有数据验证
- linux - 跳过 Sendmail 的队列
- django - 如何在 django 中设置多个自定义应用程序的路径?