当前位置: 首页 > 知识库问答 >
问题:

使用Python[duplicate]解析xml文件中的特定元素

谯嘉懿
2023-03-14

我无法使用python检索以下xml中的性别字段。我试过以下方法:

import xml.etree.ElementTree as ET
requests.get('http://www.librarything.com/services/rest/1.1/method=librarything.ck.getauthor&id=216&apikey=d231aa37c9b4f5d304a60a3d0ad1dad4')
root = ET.fromstring(req.text)
print(root.find(".//field[@type='5']"))

我希望得到元素。但我得到“没有”

<response stat="ok">
<ltml xmlns="http://www.librarything.com/" version="1.1">
<item id="216" type="author">
<author id="216" authorcode="clarkesusanna">...</author>
<url>http://www.librarything.com/author/216</url>
<commonknowledge>
<fieldList>
<field type="22" name="canonicalname" displayName="Canonical name">...</field>
<field type="20" name="biography" displayName="Short biography">...</field>
<field type="33" name="relationships" displayName="Relationships">...</field>
<field type="18" name="nationality" displayName="Nationality">...</field>
<field type="32" name="othernames" displayName="Other names">...</field>
<field type="17" name="occupations" displayName="Occupations">...</field>
<field type="9" name="education" displayName="Education">...</field>
<field type="6" name="placesofresidence" displayName="Places of residence">...</field>
<field type="44" name="birthplace" displayName="Birthplace">...</field>
<field type="31" name="legalname" displayName="Legal name">...</field>
<field type="4" name="awards" displayName="Awards and honors">...</field>
<field type="8" name="birthdate" displayName="Birthdate">...</field>
<field type="5" name="gender" displayName="Gender">
<versionList>
<version id="7537" archived="0" lang="eng">
<date timestamp="1191988667">Tue, 09 Oct 2007 23:57:47 -0400</date>
<person id="1496">
<name>felius</name>
<url>http://www.librarything.com/profile/felius</url>
</person>
<factList>
<fact>female</fact>
</factList>
</version>
</versionList>
</field>
</fieldList>
</commonknowledge>
</item>
<legal>
By using this data you agree to the LibraryThing API terms of service.
</legal>
</ltml>
</response>

XML页面

有人能帮我理解我做错了什么吗?

共有1个答案

齐思淼
2023-03-14

您应该测试的第一件事是简化XPath时会发生什么:

>>> print(root.find(".//field"))
None

那么,发生了什么事?您没有任何类型为field的元素。您有一个显式名称空间,这意味着您有'{http://www.librarything.com/}字段'。你可以很容易地看到这一点:

>>> print(root.getchildren())
[<Element '{http://www.librarything.com/}item' at 0x1047580e8>]
>>> print(root.find(".//{http://www.librarything.com/}field"))
<Element '{http://www.librarything.com/}field' at 0x1047582c8>
>>> print(root.find(".//{http://www.librarything.com/}field[@type='5']"))
<Element '{http://www.librarything.com/}field' at 0x104758688>

如果你想知道更多,这个网站上有很多关于ETree如何处理命名空间的问题(从快速搜索,1和2看起来相关),以及留档中的详细信息;试图在另一个答案中解释这一切只会导致对现有答案的劣等回答。

 类似资料:
  • 问题内容: 我如何在Java或C中使用vtd-xml解析如下的xml文件? 任何帮助,将不胜感激。 谢谢 问题答案: 我想其中有些取决于您要如何解析文件。 这是一个“非生产”示例,其中使用了一些有用的技术,包括: XPath选择(此处仅使用“ / *”) 浏览所有同级节点 通过子节点向下看 使用AutoPilot将节点属性提取到地图中 希望能帮助到你 产生以下输出: 添加带有AutoPilot循环

  • 如何在java或C中使用vtd xml解析如下xml文件? 任何帮助都将不胜感激。 谢谢

  • 我正在尝试用解析XML中不同元素的特定方法编写一些单元测试。但是我在单元测试中解析“测试”xml文件时遇到了一些问题。 我的问题并不是与XML/XSD文件有关,而是关于如何在单元测试中正确解析它们。 这是我到目前为止的代码: 即使我在实现方法中以同样的方式进行解析,我也会得到以下错误: 错误:读取文件“dir/testxsd.xsd”时出错:无法加载外部实体“dir/testxsd.xsd” 我尝

  • 我试图解析一个xml文件,但似乎无法提取我想要的部分。xml文件是为自己开发的系统准备的,我对它的布局没有任何控制权。 文件如下所示: 我希望得到这样的输出—— 我能得到name@domain因此—— 我可以用这个获得所有的值属性- 我似乎无法返回与用户相关联的角色。我想应该是这样的—— 然而,这并不返回任何东西。 关于如何获得用户的任何想法

  • 为了在巨大的xml文件中执行XPATH查询,我阅读了许多喜欢VTD-xml的文章,因此我复制了这些文章中的代码: 但当我运行它时没有结果,所以这意味着XML文件没有映射到内存中。。。我的问题是如何在VTD-xml中强制映射xml文件?

  • 本文向大家介绍Python解析xml中dom元素的方法,包括了Python解析xml中dom元素的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Python解析xml中dom元素的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的Python程序设计有所帮助。