码迷,mamicode.com
首页 > 编程语言 > 详细

Python:防止urllib2 302自动跳转

时间:2014-07-08 23:44:41      阅读:584      评论:0      收藏:0      [点我收藏+]

标签:http   color   os   cti   for   html   

例子
 
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   color   os   cti   for   html   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!