J'utilise la fonction Python urllib2.urlopen
pour lire le site Web http://www.bad.org.uk/ mais je continue à recevoir une erreur 302 même si quand je visite le site il se charge bien. Quelqu'un a une idée de pourquoi?Python urllib2.urlopen retournant 302 erreur même si la page existe
import socket
headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }
socket.setdefaulttimeout(10)
try:
req = urllib2.Request('http://www.bad.org.uk/', None, headers)
urllib2.urlopen(req)
return True # URL Exist
except ValueError, ex:
print 'URL: %s not well formatted' % 'http://www.bad.org.uk/'
return False # URL not well formatted
except urllib2.HTTPError, ex:
print 'The server couldn\'t fulfill the request for %s.' % 'http://www.bad.org.uk/'
print 'Error code: ', ex.code
return False
except urllib2.URLError, ex:
print 'We failed to reach a server for %s.' % 'http://www.bad.org.uk/'
print 'Reason: ', ex.reason
return False # URL don't seem to be alive
Erreur d'impression:
The server couldn't fulfill the request for http://www.bad.org.uk//site/1/default.aspx.
Error code: 302
Comment faire? Désolé je suis assez nouveau à Python et n'ai pas utilisé l'urllib2 avant – John
@John - C'est une question différente! –
Les 302s sont automatiquement gérés par urllib2 par défaut. –