• Python网络爬虫


    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

  • 相关阅读:
    表操作
    mysql表的完整性约束
    mysql支持的数据类型
    数据库存储引擎
    Navicat工具、pymysql模块、数据备份
    数据库一
    IO模型
    协成
    线程
    进程
  • 原文地址:https://www.cnblogs.com/davidgu/p/4813567.html
Copyright © 2020-2023  润新知