如何在 Python 中获取 XML 标记值

2022-01-10 00:00:00 python xml parsing xml-parsing dom

问题描述

我在 Python 中的 unicode-string 变量中有一些 XML,如下所示:

I have some XML in a unicode-string variable in Python as follows:

<?xml version='1.0' encoding='UTF-8'?>
<results preview='0'>
<meta>
<fieldOrder>
<field>count</field>
</fieldOrder>
</meta>
    <result offset='0'>
        <field k='count'>
            <value><text>6</text></value>
        </field>
    </result>
</results>

如何使用Python提取<value><text>6</text></value>中的6?

How do I extract the 6 in <value><text>6</text></value> using Python?


解决方案

BeautifulSoup<据我所知,/a> 是解析 XML 的最简单方法...

BeautifulSoup is the most simple way to parse XML as far as I know...

并假设您已经阅读了介绍,那么只需简单地使用:

And assume that you have read the introduction, then just simply use:

soup = BeautifulSoup('your_XML_string')
print soup.find('text').string

相关文章