2

I am trying to parse a broken html page which has a comment inside anther comment and all the famous htmlparsers like beautifulsoup, lxml and HTMLParser are giving syntax errors. Following is the code. How do I ignore the part of corrupt code and parse rest of the page?

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

2 Answers 2

3

If you know what the problem is, you can preprocess: first use a primitive method like regexps to strip the offending inner comment, then hit it with a real parser.

Sign up to request clarification or add additional context in comments.

Comments

2

I have no errors with this html. I tried beautifulsoup4 and 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.