我正在尝试解析一个损坏的 html 页面,该页面在花药注释中包含一条注释,并且所有著名的 htmlparser(如 beautifulsoup、lxml 和 HTMLParser)都给出了语法错误。以下是代码。如何忽略损坏代码的部分并解析页面的其余部分?
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script language="JavaScript">
<!--
function setTimeOffsetVars (Link) {
// code removed
}
<!-- Image Preloader - takes an array of images to preload -->
function warningCheck(e, warnMsg) {
// code removed
}
-->
</script>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<!-- lot of useful code -->
</body></html>
最佳答案
如果您知道问题是什么,您可以进行预处理:首先使用正则表达式之类的原始方法来删除有问题的内部注释,然后使用真正的解析器来处理它。
关于python - 在Python中解析损坏的html页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037866/