handle_excpetion.py

from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys def getLogo(url):
try:
html = urlopen(url)
except HTTPError as e:
print("url open exception:")
print(e)
return None try:
bsObj = BeautifulSoup(html.read(), "html.parser")
logo = bsObj.body.img
except AttributeError as e:
print("parse logo exception:")
print(e)
return None
return logo logo = getLogo("http://www.baidu2.com/nopage.html")
if logo == None:
print("Logo could not be found")
else:
print(logo)

运行结果:

url open exception:
HTTP Error 404: Not Found
Logo could not be found

最新文章

  1. 使用SVG图像作为loading加载 以保证图像高清不模糊
  2. 动态生成元素动作绑定,jquery 1.9如何实现
  3. 开源的EtherCAT Master简介
  4. matlab 中randn randi rand randsrc的用法以及区别
  5. HIbernate的脏数据检测和延缓加载
  6. HTML简历表格
  7. ORA-01000:超出打开游标的最大数(C#)
  8. win7下eclipse中文字显示过小
  9. webstorm启动bug
  10. VxWorks 6.9 内核编程指导之读书笔记 -- ISRs和Watchdog Timer
  11. C调用OPENSSL做REST服务客户端的例子
  12. 10.15_SVG可以解决问题吗
  13. cf D. Alternating Current
  14. Xcode6为什么干掉pch(Precompile Prefix Header)&怎样加入pch文件
  15. UiAutomator源码分析之注入事件
  16. 第七十九,CSS3背景渐变效果
  17. 汉化Eclipse
  18. Unresolved reference issue in PyCharm
  19. Open Live Writer
  20. Mybatis源码分析之结果封装ResultSetHandler和DefaultResultSetHandler

热门文章

  1. Hibernate *.hbm.xml对象关系映射文件详解
  2. [BZOJ3924][ZJOI2015]幻想乡战略游戏(动态点分治)
  3. Apache之.htaccess备忘录(一)
  4. 【美】范·K·萨普曼 - 通向财务自由之路(2013年11月26日)
  5. apache 与 iis共存
  6. unsigned int与int相加的问题-----C/C++小知识 区别
  7. 1Password:让一个密码记住所有密码
  8. Ext各种对话框
  9. 《Objective-C开发经典教程》
  10. (转)找回vss超级管理员密码