Python网络爬虫 - 3. 异常处理

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

 

posted @ 2015-09-16 15:54  Master HaKu  阅读(300)  评论(0编辑  收藏  举报