如何在Python中获取XML标签值 [英] How to get XML tag value in Python

查看:1520
本文介绍了如何在Python中获取XML标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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>

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

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

推荐答案

BeautifulSoup 是最简单的方法解析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

这篇关于如何在Python中获取XML标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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