繁体   English   中英

为什么无法使用urllib.urlretrieve下载图像?

[英]How come I cannot download an image with urllib.urlretrieve?

我尝试通过以下方式下载Google的徽标

import os, urllib
folderName = 'downloadedImages'
if not os.path.exists(folderName):
  os.makedirs(folderName)
urllib.urlretrieve(https://www.google.com/images/srpr/logo3w.png, './' + folderName + '/')

但是,我收到一个错误: IOError: [Errno 21] Is a directory: './downloadedImages/'

为什么?

由于错误的原因:第二个参数中提供的是目录。 它必须是本地(目标)文件名。

python docs状态(强调我的意思):

第二个参数(如果存在)指定要复制到的文件位置 (如果不存在,该位置将是具有生成名称的临时文件)。

查看文档: http : //docs.python.org/library/urllib.html#urllib.urlretrieve

您需要提供文件名,而不是目录路径:

urllib.urlretrieve("path/to/resource", os.path.join(folderName, "filename.jpg"))

urllib.urlretrieve()第二个参数需要文件名而不是目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM