BeautifulSoup第一課

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:

        obj=BeautifulSoup(html,"html.parser")

        title=obj.body.h1

    except AttributeError as e:        #屬性錯誤

        return none

    return title

title=getTitle("                     ")

if title == None:

    print("None Found")

else:

    print(title)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章