Python中BeautifulSoup中對HTML標簽的提取


一開始使用了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)
    
    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM