您的位置:首页 > 编程语言 > Python开发

BeautifulSoup使用

2015-12-30 09:42 369 查看
#python2.7
#BeautifulSoup4.0
import urllib
import urllib2
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
from urllib2 import HTTPError

def getTitle(url):
try:
html = urlopen(url)
except HTTPError as e:
return None
try:
bsObj = BeautifulSoup(html.read())
title = bsObj.title.string
except AttributeError as e:
return None
return title

title = getTitle("https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN")
if title ==None:
print ("Title counld not be found")
else:
print (title)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python