考虑异常值
思路
有些程序在运行的时候,如果没有考虑异常值,就会运行出错,因此,在写代码的时候就要考虑到异常值的出现。用try-except处理,推荐敲代码之前,先画一个思路图
代码
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:
bsObj = BeautifulSoup(html.read(), "lxml")
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)