How to parse malformed HTML in python

后端 未结 2 1091
别那么骄傲
别那么骄傲 2021-01-04 03:51

I need to browse the DOM tree of a parsed HTML document.

I\'m using uTidyLib before parsing the string with lxml

a = tidy.parseString(html_code, options) dom

相关标签:
2条回答
  • 2021-01-04 04:10

    Beautiful Soup does a good job with invalid/broken HTML

    >>> from BeautifulSoup import BeautifulSoup
    >>> soup = BeautifulSoup("<htm@)($*><body><table <tr><td>hi</tr></td></body><html")
    >>> print soup.prettify()
    <htm>
     <body>
      <table>
       <tr>
        <td>
         hi
        </td>
       </tr>
      </table>
     </body>
    </htm>
    
    0 讨论(0)
  • 2021-01-04 04:12

    Since you are already using lxml, have you tried lxml's ElementSoup module?

    If ElementSoup can't repair the HTML then you'll probably need to apply your own filters first that are based on your own observations of how the data is broken.

    0 讨论(0)
提交回复
热议问题