在python中解析损坏的html页面
我试图解析一个破损的html页面,该页面在另一条注释中包含一条注释,所有著名的HTMLPasser,如beautifulsoup、lxml和HTMLPasser都给出了语法错误。下面是代码。如何忽略损坏代码的一部分并解析页面的其余部分在python中解析损坏的html页面,python,html-parsing,beautifulsoup,lxml,Python,Html Parsing,Beautifulsoup,Lxml,我试图解析一个破损的html页面,该页面在另一条注释中包含一条注释,所有著名的HTMLPasser,如beautifulsoup、lxml和HTMLPasser都给出了语法错误。下面是代码。如何忽略损坏代码的一部分并解析页面的其余部分 <html xmlns="http://www.w3.org/1999/xhtml"><head> <script language="JavaScript"> <!-- function setTimeOff
<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>
功能警告检查(e、warnMsg){
//代码删除
}
-->
如果您知道问题出在哪里,您可以进行预处理:首先使用诸如regexps之类的基本方法去除有问题的内部注释,然后使用真正的解析器对其进行分析。此html没有错误。我试过beautifulsoup4和lxml
from bs4 import BeautifulSoup
soup = BeautifulSoup(s)
print soup.prettify()
<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 bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
<!-- lot of useful code -->
</body>
</html>
从bs4导入美化组
汤=美汤(s)
打印汤。美化
!--
函数setTimeOffsetVars(链接){
//代码删除
}
!-- 图像预加载程序-获取要预加载的图像数组--
功能警告检查(e、warnMsg){
//代码删除
}
--