【发布时间】:2014-08-05 02:24:32
【问题描述】:
我有一个代码和一个问题。
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。如果它没有找到图像,它应该这样说并增加 nothingfoundnumber。
现在它不起作用,它只是说它总是找到一些东西并且什么都不保存。 有人可以帮我解决这个问题吗?
【问题讨论】:
-
url 检索返回一个元组(文件名,httplib.HTTPMessage 实例)。您的 404 在后者中。请看stackoverflow.com/questions/1308542/…
标签: python image download urllib