面對網絡不穩定,頁面更新等問題,很可能出現程序異常的問題,所以我們要對程序進行一些異常處理。大家可能覺得處理異常是一個比較麻煩的活,但在面對復雜網頁和任務的時候,無疑成為一個很好的代碼習慣。
網頁‘404'、‘500'等問題
try: html = urlopen('http://www.pmcaff.com/2221') except HTTPError as e: print(e)
返回的是空網頁
if html is None: print('沒有找到網頁')
目標標簽在網頁中缺失
try: #不存在的標簽 content = bsObj.nonExistingTag.anotherTag except AttributeError as e: print('沒有找到你想要的標簽') else: if content == None: print('沒有找到你想要的標簽') else: print(content)
實例
if sys.version_info[0] == 2: from urllib2 import urlopen # Python 2 from urllib2 import HTTPErrorelse: from urllib.request import urlopen # Python3 from urllib.error import HTTPErrorfrom bs4 import BeautifulSoupimport sysdef getTitle(url): try: html = urlopen(url) except HTTPError as e: print(e) return None try: bsObj = BeautifulSoup(html.read()) title = bsObj.body.h1 except AttributeError as e: return None return titletitle = getTitle("http://www.pythonscraping.com/exercises/exercise1.html")if title == None: print("Title could not be found")else: print(title)
以上全部為本篇文章的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答