您的位置:首页 > 编程语言 > Python开发

使用python将远程图片下载到本地

2014-12-09 09:45 471 查看
使用如下方法,图片下载后不能打开:

img = urllib2.urlopen(<span style="color: rgb(128, 0, 0); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; font-size: 14px; line-height: 17.8048000335693px; background-color: transparent;">'http://example.com/img.png'</span>)
fname = 1.jpg'
with open(fname, 'wb') as localFile:
localFile.write(img.read())


使用如下方法,图片下载正常:

fname = '1.jpg'
urllib.urlretrieve(<span style="color: rgb(128, 0, 0); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; font-size: 14px; line-height: 17.8048000335693px; background-color: transparent;">'http://example.com/img.png'</span>, fname)


urllib.urlretrieve函数的作用是直接将远程数据下载到本地
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: