首页 > 代码库 > BeautifulSoup第一课

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)


本文出自 “祝融与火” 博客,请务必保留此出处http://notezr.blog.51cto.com/9424982/1893270

BeautifulSoup第一课