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

2016-05-14 22:26:59 +08:00
 Cola90
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
3851 次点击
所在节点    Python
5 条回复
zwzmzd
2016-05-14 22:39:07 +08:00
看样子 503 页面在 urlopen 页面就报异常了,所以 response 没有得到赋值

换 requests 库试试,没记错的话应该可以
Slienc7
2016-05-14 22:42:09 +08:00
BOYPT
2016-05-14 22:47:39 +08:00
response = e
Cola90
2016-05-14 22:53:32 +08:00
solved !谢谢楼上各位!
YUX
2016-05-14 23:03:31 +08:00
r = requests.get('https://api.github.com/user')
r.status_code

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/278683

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX