python3 中解析简单的xml文档-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

**
xml是一种十分常见的表机性语言,可提供统一的方法来描述应用程序的结构化数据。
**
[code lang="xml"]
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>

那么在python当中如何解析xml文档呢?

我们可以使用标准库中的xml.etree.ElementTree,其中的parse函数可以解析xml文档。

from xml.etree.ElementEtree import parse #导入这个函数

parse这个函数有两个参数parse(source,parse=None)

可以把上面这个xml文件作为source也就是输入元。

HTTPX 基础教程-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客