您的位置:首页 > 其它

创建爬虫-----爬虫异常处理:

2017-06-30 14:30 197 查看

爬虫异常处理:

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/pages1.html")
if title==None:
print("title could not be found")
else:
print(title)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  爬虫 异常处理