您的位置:首页 > 编程语言 > Python开发

python之获取页面标签的方法

2016-10-09 13:59 411 查看
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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: