码迷,mamicode.com
首页 > 其他好文 > 详细

BeautifulSoup第一课

时间:2017-01-20 13:27:54      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:beautifulsoup   python3   

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第一课

标签:beautifulsoup   python3   

原文地址:http://notezr.blog.51cto.com/9424982/1893270

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!