python - 使用 python 时 xml 中缺少属性

标签 python xml python-requests

我正在尝试阅读此内容url并尝试提取此标签之间的信息:“identificationInfo”

但是,当我使用此代码时:

import requests
import xml.etree.ElementTree as ET

url = "http://qldspatial.information.qld.gov.au/catalogue/rest/document?id={96BD66CE-2207-4D35-815B-0E5648C0185F}&f=xml"

response = requests.get(url)

xml_content = response.content

tree = ET.fromstring(xml_content)

for child in tree:

    print(child.tag, child.attrib)

但是我返回的结果不包含标签的任何属性。

('{http://www.isotc211.org/2005/gmd}fileIdentifier', {})
('{http://www.isotc211.org/2005/gmd}language', {})
('{http://www.isotc211.org/2005/gmd}characterSet', {})
('{http://www.isotc211.org/2005/gmd}parentIdentifier', {})
('{http://www.isotc211.org/2005/gmd}hierarchyLevel', {})
('{http://www.isotc211.org/2005/gmd}contact', {})
('{http://www.isotc211.org/2005/gmd}dateStamp', {})
('{http://www.isotc211.org/2005/gmd}metadataStandardName', {})
('{http://www.isotc211.org/2005/gmd}metadataStandardVersion', {})
('{http://www.isotc211.org/2005/gmd}referenceSystemInfo', {})
('{http://www.isotc211.org/2005/gmd}identificationInfo', {})
('{http://www.isotc211.org/2005/gmd}distributionInfo', {})
('{http://www.isotc211.org/2005/gmd}dataQualityInfo', {})
('{http://www.isotc211.org/2005/gmd}metadataConstraints', {})`

我对xml不熟悉,我不明白为什么我看不到更多信息。我是不是少了一步?如果有人可以提供帮助,我们将不胜感激。

最佳答案

我使用的是 minidom 而不是 ElementTree。获取所需值的代码是:

from xml.dom import minidom
import requests

url = "http://qldspatial.information.qld.gov.au/catalogue/rest/document?id={96BD66CE-2207-4D35-815B-0E5648C0185F}&f=xml"

response = requests.get(url)
xml_content = response.content
doc = minidom.parseString(xml_content)
identification = doc.getElementsByTagName("identificationInfo")[0]
date = identification.getElementsByTagName('gco:Date')[0].firstChild.nodeValue # "2014-09-05"
responsible_party = identification.getElementsByTagName('CI_ResponsibleParty')[0]
department = responsible_party.getElementsByTagName('gco:CharacterString')[0].firstChild.nodeValue # "Department of National Parks, Sport and Racing"

关于python - 使用 python 时 xml 中缺少属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082341/

相关文章:

xml - 如何使用 Node 从 URL 解析 XML

python - 尝试解析 JS 脚本中的特定值

python - 与 python 中的 ganache-cli 同步

python - 是否可以将数据透视表转换为普通表?

python - 在字典声明中使用 OrderedDict

c++ - 如何在 C++ 中将整个 xml 文件源存储为字符串

python - 带 2 位小数的舍入方法的错误结果

android - Android list 中的 strings.xml 冲突

python - 使用正确的返回值修补相同的对象

Python 请求 : Measure time waiting for response