2014-10-01 51 views
0

我使用python中的urllib2获取url内容,并将它们置于python的本机html分析器中。代码奇妙的作品以及我的蟒蛇2.7.4,但是,我的朋友的机器已经蟒蛇2.6.9和问题,面临他的机器上是:格式不正确的开始标记,在python 2.6.9中抛出异常,但在2.7.4中抛出异常HTMLParser

Traceback (most recent call last): 
File "opsview_audit.py", line 420, in <module> 
check_instances_against_regex(instances) 
File "opsview_audit.py", line 219, in check_instances_against_regex 
attrs_being_monitored = get_host_monitoring_status(cred['url'], running_instances, 
cred['user_name'], cred['pass_key']) 
File "opsview_audit.py", line 112, in get_host_monitoring_status 
parser.feed(result.read()) 
File "/usr/lib64/python2.6/HTMLParser.py", line 108, in feed 
self.goahead(0) 
File "/usr/lib64/python2.6/HTMLParser.py", line 148, in goahead 
k = self.parse_starttag(i) 
File "/usr/lib64/python2.6/HTMLParser.py", line 229, in parse_starttag 
endpos = self.check_for_whole_start_tag(i) 
File "/usr/lib64/python2.6/HTMLParser.py", line 304, in check_for_whole_start_tag 
self.error("malformed start tag") 
File "/usr/lib64/python2.6/HTMLParser.py", line 115, in error 
raise HTMLParseError(message, self.getpos()) 
HTMLParser.HTMLParseError: malformed start tag, at line 509, column 47 

可能有些开始标签不正确的,这在python 2.6.9作为例外抛出,但不在2.7.4
这里,2.6.9升级到2.7.4或更高版本不是一种选择。

回答

0

两种解决方案:

- 使用另一个htmlparser,如Beautiful soup 3或lxml。他们都很容易学习和Python 2.6的兼容。

- 试图找到该错误并将其过滤掉。

+0

美丽的汤不是一个选项。如果我切换到BS,会有很多依赖项会受到影响。 – 2014-10-01 09:30:23

+0

然后剩下的唯一选择是试图在html中查找错误。 – 2014-10-01 09:31:54

+0

尝试通过放置try:catch:来试图调试问题,并尝试通过放置try:catch:pass来忽略错误,但是我的htmlparser不会进一步解析(远程抓取的html页面,我无法控制它)。 – 2014-10-01 09:32:06