为什么这个图片下载脚本不起作用?

时间:2014-06-14 19:55:12

标签: python image download urllib

我有一个代码和一个问题。

import string
import random
import httplib
import urllib
import os
import sys
import inspect

def id_generator(size=5, chars=string.ascii_letters + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


picnumber = raw_input('Please enter the amount of images you want!')
nothingfoundnumber=0
foundnummer=0

scriptpath = os.path.dirname(sys.argv[0])
filename = scriptpath + "/output/"
if not os.path.exists(os.path.dirname(filename)):
    os.makedirs(os.path.dirname(filename))


while foundnummer != picnumber:
    randompicstring = id_generator()
    print "Trying " + str(randompicstring)
    try:
        urllib.urlretrieve("http://i.imgur.com/" +randompicstring+ ".gif", "/test/" +randompicstring + ".gif")
        foundnummer+=1
        print str(randompicstring) + "was found! Makes " +str(foundnummer)+ " out of " +str(picnumber)+"!"
    except IOError:
        nothingfoundnumber+=1
        print str(randompicstring) + "not found. It was the "+str(nothingfoundnumber)+" try."

这样做的目的是尝试随机组合asciiletters和数字以在imgur.com上找到图像(例如http://i.imgur.com/XgEVx.png)。如果它找到了它应该说的东西并保存图像并增加foundnumber。如果找不到图像,则应该说明并增加无用的数字。

现在它不起作用,它只是说它总能找到一些东西并且什么也不保存。 有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您还应该考虑使用Imgur API而不是生成随机字符串。看起来随机图像有一个端点。

答案 1 :(得分:0)

这可能是因为urlretrieve在发生错误404时不会引发异常。您可以在urlopen之前尝试urlretrieve,看看这是否是404:

randompicstring = id_generator()
print "Trying " + str(randompicstring)
url = "http://i.imgur.com/" +randompicstring+ ".gif"

res = urllib.urlopen(url) # test url
if res.getcode() == 200: # valid link
    try:
        urllib.urlretrieve(url, "/test/" +randompicstring + ".gif") # download
        foundnummer+=1
        print str(randompicstring) + "was found! Makes " +str(foundnummer)+ " out of " +str(picnumber)+"!"
    except IOError:
        print "IOError..."
else: # invalid link
    nothingfoundnumber+=1
    print str(randompicstring) + "not found. It was the "+str(nothingfoundnumber)+" try."
相关问题