Python网络爬虫 - 3. 异常处理
2024-10-20 16:05:00
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
最新文章
- 使用SVG图像作为loading加载 以保证图像高清不模糊
- 动态生成元素动作绑定,jquery 1.9如何实现
- 开源的EtherCAT Master简介
- matlab 中randn randi rand randsrc的用法以及区别
- HIbernate的脏数据检测和延缓加载
- HTML简历表格
- ORA-01000:超出打开游标的最大数(C#)
- win7下eclipse中文字显示过小
- webstorm启动bug
- VxWorks 6.9 内核编程指导之读书笔记 -- ISRs和Watchdog Timer
- C调用OPENSSL做REST服务客户端的例子
- 10.15_SVG可以解决问题吗
- cf D. Alternating Current
- Xcode6为什么干掉pch(Precompile Prefix Header)&;amp;怎样加入pch文件
- UiAutomator源码分析之注入事件
- 第七十九,CSS3背景渐变效果
- 汉化Eclipse
- Unresolved reference issue in PyCharm
- Open Live Writer
- Mybatis源码分析之结果封装ResultSetHandler和DefaultResultSetHandler
热门文章
- Hibernate *.hbm.xml对象关系映射文件详解
- [BZOJ3924][ZJOI2015]幻想乡战略游戏(动态点分治)
- Apache之.htaccess备忘录(一)
- 【美】范·K·萨普曼 - 通向财务自由之路(2013年11月26日)
- apache 与 iis共存
- unsigned int与int相加的问题-----C/C++小知识 区别
- 1Password:让一个密码记住所有密码
- Ext各种对话框
- 《Objective-C开发经典教程》
- (转)找回vss超级管理员密码