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!

 

posted @ 2016-10-09 13:59  知_行  阅读(2643)  评论(0编辑  收藏  举报