疑似BUG:
SGMLParser处理html标签中的javascript时特定情况下失当
库: Python2.4/2.5的sgmllib库
牵连库: Beautiful Soup version 3.0.5 以及3.0.3版本
举例:
html代码如下定义:
sExceptionHtml
=
'''
<span>出错的html标签:</span><div id='error'>
<img 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>
'''
这个img标签有两个属性:onload和onmouseover,里面都写的是javascript代码,并且出现了“>”判断符号。当让SGMLParser处理这种html代码时,它错误地解析了。
对于上面的html代码,会得到如下处理过后的img:
<img 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
CTRL+Mouse wheel to zoom in/out';}" /> 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
CTRL+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
CTRL+Mouse wheel to zoom in/out';}"
/>
显然,onmouseover的东西乱掉了。很有可能就是javascript中的“this.width>screen.width*0.7”中“>”被误当作html标签的结束符处理了。
如果确实是这样,倒也可以理解,只是咱们就受累些,之前提前清除onload属性和onmouseover属性吧,省得里面的javascript干扰。
page_content = re.sub('onload=\"\s*[^\"]*\"','',page_content)
page_content = re.sub('onmouseover=\"\s*[^\"]*\"','',page_content)
牵连影响: 并因此影响到了 Beautiful Soup 对html的解析。
你可以测试如下代码,可以重现此问题:
#
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" 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
'
-----------------------
'
库: Python2.4/2.5的sgmllib库
牵连库: Beautiful Soup version 3.0.5 以及3.0.3版本
举例:
html代码如下定义:







对于上面的html代码,会得到如下处理过后的img:
<img 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
CTRL+Mouse wheel to zoom in/out';}" /> 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
CTRL+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
CTRL+Mouse wheel to zoom in/out';}"
/>
显然,onmouseover的东西乱掉了。很有可能就是javascript中的“this.width>screen.width*0.7”中“>”被误当作html标签的结束符处理了。
如果确实是这样,倒也可以理解,只是咱们就受累些,之前提前清除onload属性和onmouseover属性吧,省得里面的javascript干扰。
page_content = re.sub('onload=\"\s*[^\"]*\"','',page_content)
page_content = re.sub('onmouseover=\"\s*[^\"]*\"','',page_content)
牵连影响: 并因此影响到了 Beautiful Soup 对html的解析。
你可以测试如下代码,可以重现此问题:












































































结论:不是什么严重问题。只是当html代码中在标签的属性中写javascript时,需要注意到此种特性,如果出现“>”符号,就会导致SGMLParser以及使用SGMLParser的其他库解析失当。zhengyun 20080115