忽略 Python 中的 XML 错误 [英] Ignoring XML errors in Python

查看:24
本文介绍了忽略 Python 中的 XML 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 XML minidom (xml.dom.minidom),但是 XML 中的任何错误都会杀死解析器.是否可以忽略它们,例如浏览器?我正在尝试用 Python 编写浏览器,但如果标签不完全兼容,它只会抛出异常.

I am using XML minidom (xml.dom.minidom) in Python, but any error in the XML will kill the parser. Is it possible to ignore them, like a browser for example? I am trying to write a browser in Python, but it just throws an exception if the tags aren't fully compatible.

推荐答案

有一个名为 BeautifulSoup,我想这就是你要找的.当您尝试解析无效的 XML 时,普通的 XML 解析器将无法工作.BeautifulSoup 更容​​错,它仍然可以从无效的 XML 中提取信息.

There is a library called BeautifulSoup, I think it's what you're looking for. As you're trying to parse a invalid XML, the normal XML parser won't work. BeautifulSoup is more fail-tolerant, it can still extract information from invalid XML.

Beautiful Soup 是一个 Python HTML/XML专为快速周转而设计的解析器屏幕抓取等项目.三功能使其功能强大:

Beautiful Soup is a Python HTML/XML parser designed for quick turnaround projects like screen-scraping. Three features make it powerful:

  1. 如果你给它不好的标记,Beautiful Soup 不会窒息.它产生一个解析树大约为很有意义作为您的原始文件.这通常足以收集您需要的数据却跑掉了.
  2. Beautiful Soup 提供了一些简单的方法和 Pythonic 习惯用法导航、搜索和修改解析树:用于剖析一个的工具包文档并提取您需要的内容.您不必创建自定义每个应用程序的解析器.
  3. Beautiful Soup 自动将传入的文档转换为 Unicode和传出文件到 UTF-8.你不必考虑编码,除非文档没有指定编码和美汤不能自动检测一个.然后你只需要指定原始编码.

Beautiful Soup 解析你的任何东西给它,并进行树遍历给你的东西.你可以告诉它找到所有链接",或查找所有链接类 externalLink",或查找所有网址与foo.com"匹配的链接,或找到表格标题粗体文本,然后给我那个文本."

Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. You can tell it "Find all the links", or "Find all the links of class externalLink", or "Find all the links whose urls match "foo.com", or "Find the table heading that's got bold text, then give me that text."

这篇关于忽略 Python 中的 XML 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆