我使用Python函数urllib2.urlopen
来阅读http://www.bad.org.uk/网站,但即使访问网站时它仍然会收到302错误,但它仍然可以正常加载。任何人有任何想法为什么?Python urllib2.urlopen即使页面存在也返回302错误
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
错误印刷:
The server couldn't fulfill the request for http://www.bad.org.uk//site/1/default.aspx.
Error code: 302
我该怎么做?对不起,我对Python很陌生,在 – John 2010-11-04 16:19:35
之前没有使用过urllib2 @John - 这是另外一个问题! – 2010-11-04 16:27:17
302s默认由urllib2自动处理。 – 2010-11-04 16:32:27