python之获取页面标签的方法

from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup

 

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        return None
    try:
        bs0bj = BeautifulSoup(html.read(), "html.parser")
        title = bs0bj.head.title
    except AttributeError as e:
        return None
    return title

title = getTitle("http://www.baidu.com")
if title == None:
    print("Title could not be found !")
else:
    print(title)

结果如下图所示

END!

原文地址:https://www.cnblogs.com/changbo/p/5941982.html