扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:郑昀 来源:CSDN 2008年1月26日
关键字: 失当 JavaScript html 处理 SGMLParser
sExceptionHtml = '''<span>出错的html标签:</span><div id='error'>
<img src="http://www.onejoo.com/daylife_media/images/articlesid/1.jpg" temp_src="http://www.onejoo.com/daylife_media/images/articlesid/1.jpg" border="0"
onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7;
this.style.cursor='hand';this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}"
onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7;
this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}"
/>寒!<br /></div>'''
#coding=utf-8
import sys, os, urllib, re
from sgmllib import SGMLParser
from BeautifulSoup import BeautifulSoup
def replaceHTMLTag(content):
htmlextractor = html2txt()
# 调用定义在 SGMLParser 中的 feed 方法,将 HTML 内容放入分析器中。
htmlextractor.feed(content)
# 应该 close 您的分析器对象,但出于不同的原因。feed 方法不保证对传给它的全部 HTML 进行处理,
# 它可能会对其进行缓冲处理,等待接收更多的内容。一旦没有更多的内容,应调用 close 来刷新缓冲区,并且强制所有内容被完全处理。
htmlextractor.close()
# 一旦分析器被 close,分析过程也就结束了。htmlextractor.urls 中包含了在 HTML 文档中所有的链接 URL。
return htmlextractor.text
# 为了从 HTML 文档中提取数据,将 SGMLParser 类进行子类化,然后对想要捕捉的标记或实体定义方法。
class html2txt(SGMLParser):
def __init__(self):
SGMLParser.__init__(self)
self._result = []
self._data_stack = []
'''
reset 由 SGMLParser 的 __init__ 方法来调用,也可以在创建一个分析器实例时手工来调用。
所以如果您需要做初始化,在 reset 中去做,而不要在 __init__ 中做。
这样当某人重用一个分析器实例时,会正确地重新初始化。
'''
def reset(self):
self.text = ''
self.inbody = True
SGMLParser.reset(self)
def handle_data(self,text):
if self.inbody:
self.text += text
def _write(self, d):
if len(self._data_stack) < 2:
target = self._result
else:
target = self._data_stack[-1]
if type(d) in (list, tuple):
target += d
else:
target.append(str(d))
def start_head(self,text):
self.inbody = False
def end_head(self):
self.inbody = True
def _get_result(self):
return "".join(self._result).strip()
result = property(_get_result)

# 应用入口
if __name__ == '__main__':
sExceptionHtml = '''<span>出错的html标签:</span><div id='error'>
<img src="http://www.onejoo.com/daylife_media/images/articlesid/1.jpg" temp_src="http://www.onejoo.com/daylife_media/images/articlesid/1.jpg" border="0"
onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7;
this.style.cursor='hand';this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}"
onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7;
this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}"
/>寒!<br /></div>'''
soup = BeautifulSoup(sExceptionHtml,fromEncoding='gbk')
body_content = soup.findAll('div',attrs={'id' : re.compile("^error")})
print '----------------------'
print body_content[0]
print '----------------------'
sExceptionHtml = replaceHTMLTag(sExceptionHtml).strip()
print '----------------------'
print sExceptionHtml
print '-----------------------'
结论:不是什么严重问题。只是当html代码中在标签的属性中写javascript时,需要注意到此种特性,如果出现“>”符号,就会导致SGMLParser以及使用SGMLParser的其他库解析失当。
查看本文来源
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。