我使用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