首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过urllib和python下载图片

通过urllib和python下载图片
EN

Stack Overflow用户
提问于 2010-06-15 13:35:54
回答 19查看 381.7K关注 0票数 210

因此,我正在尝试创建一个Python脚本,用于下载网络漫画并将其放入我桌面上的一个文件夹中。我在这里找到了几个类似的程序,它们做了类似的事情,但都不是我所需要的。我发现最相似的是这里(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’递增到最大数字,这将不得不以某种方式确定。对于最好的方法或者如何正确下载文件,有什么建议吗?

谢谢!

编辑10年6月15日

这是完整的脚本,它将文件保存到您选择的任何目录。出于某种奇怪的原因,这些文件没有下载,但它们确实下载了。任何关于如何清理它的建议都将不胜感激。我目前正在研究如何找出网站上存在的许多漫画,这样我就可以获得最新的漫画,而不是让程序在引发一定数量的异常后退出。

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
EN

回答 19

Stack Overflow用户

发布于 2010-06-15 13:40:36

import urllib
f = open('00000001.jpg','wb')
f.write(urllib.urlopen('http://www.gunnerkrigg.com//comics/00000001.jpg').read())
f.close()
票数 93
EN

Stack Overflow用户

发布于 2013-02-20 00:26:25

仅供记录,使用请求库。

import requests
f = open('00000001.jpg','wb')
f.write(requests.get('http://www.gunnerkrigg.com//comics/00000001.jpg').content)
f.close()

尽管它应该检查requests.get()错误。

票数 78
EN

Stack Overflow用户

发布于 2017-07-30 22:48:55

对于Python3,您需要导入import urllib.request

import urllib.request 

urllib.request.urlretrieve(url, filename)

有关更多信息,请查看link

票数 40
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3042757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档