python - 通过urllib和python下载图片

标签 python urllib2 urllib

所以我正在尝试制作一个 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'增加到最大数字,我必须以某种方式确定。有关执行此操作的最佳方法或如何正确下载文件的任何建议?

谢谢!

2010 年 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

最佳答案

Python 2

使用 urllib.urlretrieve

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

Python 3

使用 urllib.request.urlretrieve (Python 3 遗留接口(interface)的一部分,工作方式完全相同)

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

关于python - 通过urllib和python下载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3042757/

相关文章:

python - 如何使用 urllib 和 regex 获取公司的市值数据?

python - 将返回的 urlopen() 转换为 float

python - django 使用原始 mysql 进行搜索

python - 在 Python 中使用 urllib.request.urlopen 时出错

python - ValueError : unknown url type in urllib2, 虽然如果在浏览器中打开 url 很好

Python urllib2 强制 IPv4

python - 用python将一些网站的HTML保存在一个txt文件中

python - 检查列是否在列表中,如果没有则删除并将值添加到新列

python - 使用 ctypes 在 Python 中右键单击

python - 替换 urllib2