2

Anther コメント内にコメントがある壊れた HTML ページを解析しようとしています。beautifulsoup、lxml、HTMLParser などの有名な HTML パーサーはすべて構文エラーを出しています。以下はコードです。破損したコードの部分を無視してページの残りの部分を解析するにはどうすればよいですか?

<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>
4

2 に答える 2

3

問題が何であるかがわかっている場合は、前処理を行うことができます。まず、regexps などのプリミティブ メソッドを使用して、問題のある内部コメントを削除し、次に実際のパーサーでヒットします。

于 2012-12-26T08:24:05.550 に答える
2

この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">
   &lt;!--
     function setTimeOffsetVars (Link) { 
   // code removed
 } 

&lt;!-- Image Preloader - takes an array of images to preload --&gt; 
    function warningCheck(e, warnMsg) {
   // code removed
}
--&gt;
  </script>
 </head>
 <body bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
  <!-- lot of useful code -->
 </body>
</html>
于 2012-12-26T09:00:59.100 に答える