for index in range(1,10):
send_headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.2;rv:16.0) Gecko/20100101 Firefox/16.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Connection':'keep-alive'
}
try:
req=urllib2.Request(url,headers=send_headers)
response=urllib2.urlopen(req)
sleeptime=random.randint(1,30*index)
time.sleep(sleeptime)
except Exception, e:
print e
traceback.print_exc()
sleeptime=random.randint(13,40*index)
print url
time.sleep(sleeptime)
continue
if response.getcode() != 200:
continue
else:
break
return response.read()
Ich fand, dass mein Code Schlaf auf Rückkehr response.read()
manchmal, aber das Programm ist nicht tot und es gibt keine Fehler oder eine Ausnahme, und ich weiß nicht, warum und wie es passiert. Wie kann ich es reparieren?mein Code schläft auf response.read() Funktion, manchmal
Es ist Python, ich möchte ein Bild im Web bekommen.
Ihre Frage ist unklar. Sie haben eine 'time.sleep()' Anweisung, deshalb schläft sie für eine zufällige Menge an Zeit. – Selcuk
Ich debugge meinen Code, und ich fand, dass es bei response.read() ohne Fehler, keine Ausnahme gestoppt wurde – mengyeer