如何在Python中解析格式错误的HTML
我需要浏览已解析的 HTML 文档的 DOM 树。
解析字符串之前使用 uTidyLib
我在使用 lxml a = tidy.parseString(html_code, options) dom = etree.fromstring(str(a))
有时我会收到错误,似乎 tidylib 无法修复格式错误的 html。
如何解析每个 HTML 文件而不出现错误(仅解析文件中无法修复的某些部分)?
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 = etree.fromstring(str(a))
sometimes I get an error, it seems that tidylib is not able to repair malformed html.
how can I parse every HTML file without getting an error (parsing only some parts of files that can not be repaired)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Beautiful Soup 可以很好地处理无效/损坏的 HTML
Beautiful Soup does a good job with invalid/broken HTML
既然您已经在使用 lxml,那么您是否尝试过 lxml 的 ElementSoup 模块?
如果 ElementSoup 无法修复 HTML,那么您可能需要首先应用自己的过滤器,这些过滤器基于您自己对数据损坏方式的观察。
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.