urllib2防止页面跳转
#!/usr/bin/python# -*- coding: utf-8 -*-# encoding=utf-8# Filename:states_code.pyimport urllib2class RedirctHandler(urllib2.HTTPRedirectHandler):def http_error_301(self, req, fp, code, msg, hea
·
#!/usr/bin/python
# encoding=utf-8
import urllib2
class RedirctHandler(urllib2.HTTPRedirectHandler):
def http_error_301(self, req, fp, code, msg, headers):
pass
def http_error_302(self, req, fp, code, msg, headers):
pass
def getUnRedirectUrl(url, timeout=10):
debug_handler = urllib2.HTTPHandler(debuglevel=0)
opener = urllib2.build_opener(debug_handler, RedirctHandler)
try:
response = opener.open(url, timeout=timeout)
print response.code
except urllib2.URLError as e:
if hasattr(e, 'code'):
print e.code
elif hasattr(e, 'reason'):
print e.reason
getUnRedirectUrl('http://www.baidu.com/App_Themes/Default/css/general.css')
更多推荐
已为社区贡献7条内容
所有评论(0)