【发布时间】:2010-12-16 12:28:03
【问题描述】:
我一直在尝试让 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 上运行。
【问题讨论】:
-
感谢指点!恢复到 BeautifulSoup 3.0.7a 解决了这个问题,它能够处理 3.1.0.1 阻塞的其他狡猾的 html 位。
标签: python beautifulsoup html-parsing