python - 爬到图片的URL,如何下载到本地?
PHPz
PHPz 2017-04-18 09:23:36
0
7
305

已经爬去到图片的URL,测试输出成功,如何才能把图片下载到本地?
方法一:

结果是

方法二:


结果是

新手求解,希望大神指出错误,给出改正方法,谢谢

PHPz
PHPz

学习是最好的投资!

reply all(7)
洪涛

The open function opens a file. The question is, it is a directory.

>>> fp = open("git") #git是目录
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 13] Permission denied: 'git'

Just add the file name.

迷茫
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)

Obviously the file name was not included when writing

洪涛

There is no downloaded file name...

请输入代码
def getImage(imList):  
    print 'Downloading...'  
    name = 1;  
    for imgurl in imList:  
        urllib.urlretrieve(imgurl, '%s.jpg' % name)  
        name += 1  
    print 'Got ', len(imList), ' images!' 
伊谢尔伦

Don’t use Chinese for the folder path, try changing it to an English path

小葫芦

Please confirm whether writing a file directly using python can be successful. The error is caused by permissions.

Replace the double quotes in the directory path with single quotes

洪涛

If there are Chinese characters in the path name, it is recommended to use Unicode format so that there will be no recognition errors

迷茫

urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

This function. Simple and practical. It’s better to add exception handling

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template