通过urllib和python下载图片

问题:

所以我试图制作一个Python脚本来下载webcomics并将它们放在桌面上的文件夹中。我在这里找到一些类似的程序,做类似的事情,但没有什么像我需要的。我发现最相似的就在这里(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images)。我试过使用这个代码:

>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)

然后我搜索我的电脑一个文件“00000001.jpg”,但我发现的是所有的缓存的图片。我甚至不确定它将文件保存到我的电脑。一旦我了解如何获取文件下载,我想我知道如何处理其余的。基本上只使用一个for循环,并将字符串分割为’00000000’。’jpg’,并将’00000000’递增到最大的数字,我将不得不以某种方式确定。任何建议最好的方式做这个或如何正确下载文件?
谢谢!
编辑6/15/10
这是完成的脚本,它将文件保存到您选择的任何目录。由于一些奇怪的原因,文件没有下载,他们只是做了。关于如何清理它的任何建议将不胜感激。我正在研究如何发现网站上存在的许多漫画,所以我可以得到最新的漫画,而不是在一定数量的异常提出后退出程序。

import urllib
import os

comicCounter=len(os.listdir('/file'))+1  # reads the number of files in the folder to start downloading at the next comic
errorCount=0

def download_comic(url,comicName):
    """
    download a comic in the form of

    url = http://www.example.com
    comicName = '00000000.jpg'
    """
    image=urllib.URLopener()
    image.retrieve(url,comicName)  # download comicName at URL

while comicCounter <= 1000:  # not the most elegant solution
    os.chdir('/file')  # set where files download to
        try:
        if comicCounter < 10:  # needed to break into 10^n segments because comic names are a set of zeros followed by a number
            comicNumber=str('0000000'+str(comicCounter))  # string containing the eight digit comic number
            comicName=str(comicNumber+".jpg")  # string containing the file name
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)  # creates the URL for the comic
            comicCounter+=1  # increments the comic counter to go to the next comic, must be before the download in case the download raises an exception
            download_comic(url,comicName)  # uses the function defined above to download the comic
            print url
        if 10 <= comicCounter < 100:
            comicNumber=str('000000'+str(comicCounter))
            comicName=str(comicNumber+".jpg")
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)
            comicCounter+=1
            download_comic(url,comicName)
            print url
        if 100 <= comicCounter < 1000:
            comicNumber=str('00000'+str(comicCounter))
            comicName=str(comicNumber+".jpg")
            url=str("http://www.gunnerkrigg.com//comics/"+comicName)
            comicCounter+=1
            download_comic(url,comicName)
            print url
        else:  # quit the program if any number outside this range shows up
            quit
    except IOError:  # urllib raises an IOError for a 404 error, when the comic doesn't exist
        errorCount+=1  # add one to the error count
        if errorCount>3:  # if more than three errors occur during downloading, quit the program
            break
        else:
            print str("comic"+ ' ' + str(comicCounter) + ' ' + "does not exist")  # otherwise say that the certain comic number doesn't exist
print "all comics are up to date"  # prints if all comics are downloaded

回答:

使用urllib.urlretrieve

import urllib
urllib.urlretrieve("http://www.gunnerkrigg.com//comics/00000001.jpg", "00000001.jpg")

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: Downloading a picture via urllib and python

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

8 + = 10