我正在尝试用Python构建一个程序,该程序将从一个网站获取图像并下载,但该网站返回以下链接:
http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5
它没有*.jpg或*.png扩展名。所以当我尝试使用以下代码时:
import urllib , urllib2, os , sys , time
img = urllib.urlopen("http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5").read()
hand0 = open("test.jpg" , "w")
hand0.write(img)
hand0.close()
它写的图像如下所示:
http://i.stack.imgur.com/yWCBg.jpg
有人知道出什么事了吗?在
必须通过将
"wb"
传递给open()
,以二进制模式打开输出文件。如果你在使用Windows(我认为你是因为你的代码应该可以正常工作),这一点很重要了解“wb”代表什么以及在windows上为什么需要wb。在
相关问题 更多 >
编程相关推荐