首页 文章

使BeautifulSoup忽略脚本标记内的内容

提问于
浏览
1

我一直在尝试使用BeautifulSoup(3.1.0.1)来解析一个html页面,其中包含大量生成html内部标记的javascript . 一个示例片段如下所示:

<html><head><body><div>
<script type='text/javascript'>

if(ii > 0) {
html += '<span id="hoverMenuPosSepId" class="hoverMenuPosSep">|</span>'
}
html += 
'<div class="hoverMenuPos" id="hoverMenuPosId" onMouseOver=\"menuOver_3821();\" ' +
'onMouseOut=\"menuOut_3821();\">';
if (children[ii].uri == location.pathname) {
html += '<a class="hiHover" href="' +  children[ii].uri + '" ' + onClick + '>';
} else {
html += '<a class="hover" href="' +  children[ii].uri + '" ' + onClick + '>';
}
html += children[ii].name + '</a></div>';
}
}          
hp = document.getElementById("hoverpopup_3821");
hp.style.top = (parseInt(hoveritem.offsetTop) + parseInt(hoveritem.offsetHeight)) + "px";
hp.style.visibility = "Visible";
hp.innerHTML = html;
}
return false;
}
function menuOut_3821() {
timeOn_3821 =  setTimeout("showSelected_3821()",  1000)             
}
var timeOn_3821 = null;
function menuOver_3821() {
clearTimeout(timeOn_3821)
}   
function showSelected_3821() {
showChildrenMenu_3821( 
document.getElementById("flatMenuItemAnchor" + selectedPageId), selectedPageId);
}
</script>
</body>
</html>

BeautifulSoup似乎无法解决这个问题,并且在onMouseOver = ** \“** menuOver_3821(); \”周围抱怨“格式错误的开始标记” . 它似乎尝试解析脚本块中的javascript生成的xml?!?

任何想法如何使BeautifulSoup忽略脚本标签内容?

我已经看到了使用lxml的其他建议,但不能,因为它必须在Google AppEngine上运行 .

3 回答

  • 1

    恢复到BeautifulSoup 3.0.7a解决了这个问题以及3.1.0.1已经窒息的许多其他html怪异问题 .

  • 0

    我以前遇到过这种问题,而我通常做的是用 <!-- 替换 <script</script> 的每一次出现 . 这样,所有 <script></script> 标签都被注释掉了 .

  • 0

    这样可行,但是BeautifulSoup的重点在于解析你扔给它的任何标签汤,即使它的形象非常糟糕 .

相关问题