推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
Cola90
V2EX  ›  Python

Python 要如何才能读取到 503 页面?

  •  
  •   Cola90 · May 14, 2016 · 4673 views
    This topic created in 3794 days ago, the information mentioned may be changed or developed.
    try:
    response=urllib2.urlopen(request,timeout=10)
    except urllib2.HTTPError as e:
    print e.code
    fp=open('503.html','w')
    fp.write(response.read())
    fp.close()
    return

    想要处理一个 503 页面,输出到‘ 503.html ’文件。执行以上代码,显示 response 未指定。毕业设计,比较急,先谢谢了。
    UnboundLocalError: local variable 'response' referenced before assignment
    5 replies  •  2016-05-14 23:03:31 +08:00
    zwzmzd
        1
    zwzmzd  
       May 14, 2016 via Android
    看样子 503 页面在 urlopen 页面就报异常了,所以 response 没有得到赋值

    换 requests 库试试,没记错的话应该可以
    BOYPT
        3
    BOYPT  
       May 14, 2016
    response = e
    Cola90
        4
    Cola90  
    OP
       May 14, 2016
    solved !谢谢楼上各位!
    YUX
        5
    YUX  
       May 14, 2016
    r = requests.get('https://api.github.com/user')
    r.status_code
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   772 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 20:01 · PVG 04:01 · LAX 13:01 · JFK 16:01
    ♥ Do have faith in what you're doing.