from urllib.request import urlopen
from urllib.error import HTTPError,URLError
from bs4 import BeautifulSoup
def getTitle(url):
try:
html = urlopen(url)
except (HTTPError,URLError) as e:
return None
try:
bsObj = BeautifulSoup(html.read())
title = bsObj.body.h1
except AttributeError as e:
return None;
return title;
title = getTitle("http://www.pythonscraping.com/pages/page1.html")
if title == None:
print("Title could not be found")
else:
print("title is ",title)
sublime 黑底高亮,红绿 紫蓝 白色都有,尤其是紫色的None,漂亮。
terminal 调成经典的黑底绿字,爽。