擅长:python、mysql、java
<p>这可能是因为发生错误404时<code>urlretrieve</code>不会引发异常。您可以尝试在<code>urlopen</code>之前<code>urlretrieve</code>查看这是否是404:</p>
<pre><code>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."
</code></pre>