已经爬去到图片的URL,测试输出成功,如何才能把图片下载到本地?方法一:
结果是
方法二:
新手求解,希望大神指出错误,给出改正方法,谢谢
学习是最好的投资!
open函数是打开一个文件的,题主那是个目录吧.
>>> fp = open("git") #git是目录 Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 13] Permission denied: 'git'
加上文件名就好了.
import imghdr for i in imgurl: content = urllib2.urlopen(i).read() imgtype = imghdr.what('', h=content) if not imgtype: imgtype = 'txt' with open('F:\Download\{}.{}'.format(i, imgtype), 'wb') as f: f.write(content)
显然是写入的时候没有带入文件名嘛
没有下载的文件名呢...
请输入代码 def getImage(imList): print 'Downloading...' name = 1; for imgurl in imList: urllib.urlretrieve(imgurl, '%s.jpg' % name) name += 1 print 'Got ', len(imList), ' images!'
文件夹路径不要用中文,改成英文路径试试
你确认一下直用python写个文件能不能成功,出错是说权限的问题啊。
目录路径的双引号换成单引号试试
如果路径名中有中文字符 ,建议采用Unicode格式,这样不会出现识别错误
urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
这个函数。简单实用。最好加上异常处理
open函数是打开一个文件的,题主那是个目录吧.
加上文件名就好了.
显然是写入的时候没有带入文件名嘛
没有下载的文件名呢...
文件夹路径不要用中文,改成英文路径试试
你确认一下直用python写个文件能不能成功,出错是说权限的问题啊。
目录路径的双引号换成单引号试试
如果路径名中有中文字符 ,建议采用Unicode格式,这样不会出现识别错误
urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
这个函数。简单实用。最好加上异常处理