码迷,mamicode.com
首页 > 编程语言 > 详细

Python中BeautifulSoup中对HTML标签的提取

时间:2017-01-12 08:43:45      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:字符串   sys   typeerror   dal   ext   open   html   one   tde   

一开始使用了beautifulSoup的get_text()进行字符串的提取,后来一直提取失败,并提示错误为TypeError: ‘NoneType‘ object is not callable

返回了none类型,可能是对Span标签内容的提取产生错误,于是采用name.string进行字符的提取,成功。

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 11 17:21:54 2017

@author: PE-Monitor
"""
import urllib2
import BeautifulSoup
import sys

reload(sys)
sys.setdefaultencoding(utf-8)
responce = urllib2.urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
html =BeautifulSoup.BeautifulSoup(responce)
nameList=html.findAll(span,{class:{green}})
for name in nameList:
     print(name.string)
    
    

 

Python中BeautifulSoup中对HTML标签的提取

标签:字符串   sys   typeerror   dal   ext   open   html   one   tde   

原文地址:http://www.cnblogs.com/Peit/p/6274531.html

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