首页 > 编程语言 > 详细

Python:防止urllib2 302自动跳转

时间:2014-07-08 23:44:25      阅读:597      评论:0      收藏:0      [点我收藏+]
例子
 
import urllib2
 
class RedirctHandler(urllib2.HTTPRedirectHandler):
    """docstring for RedirctHandler"""
    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):
    req = urllib2.Request(url)
    debug_handler = urllib2.HTTPHandler(debuglevel = 1)
    opener = urllib2.build_opener(debug_handler, RedirctHandler)
 
    html = None
    response = None
    try:
        response = opener.open(url,timeout=timeout)
        html = response.read()
    except urllib2.URLError as e:
        if hasattr(e, ‘code‘):
            error_info = e.code
        elif hasattr(e, ‘reason‘):
            error_info = e.reason
    finally:
        if response:
            response.close()
    if html:
        return html
    else:
        return error_info
 
html = getUnRedirectUrl(http://phpno.com)
print html

Python:防止urllib2 302自动跳转,布布扣,bubuko.com

Python:防止urllib2 302自动跳转

原文:http://www.cnblogs.com/zyue/p/3830280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!