【问题标题】:Python parsing XMLPython解析XML
【发布时间】:2015-08-05 07:37:27
【问题描述】:

我已经创建了这样的根:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

这是我的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<feed gd:etag="&quot;Rn84fzVSLyt7I2A9XRVbFkwOQAE.&quot;" xmlns="http://www.w3.org/2005/Atom" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
 <id>moha****ee@gmail.com</id>
 <updated>2015-08-03T15:12:37.137Z</updated>
 <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
 <title>Mohammad Amin's Contacts</title>
 <link rel="alternate" type="text/html" href="https://www.google.com/"/>
 <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamma***ee%40gmail.com/full"/>
 <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamm***aee%40gmail.com/full"/>
 <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moha****ee%40gmail.com/full/batch"/>
 <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moham***ee%40gmail.com/full?max-results=25"/>
 <link rel="next" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moha****aee%40gmail.com/full?max-results=25&amp;start-index=26"/>
 <author>
  <name>Mohammad Amin</name>
  <email>moha****ee@gmail.com</email>
 </author>
 <generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator>
 <openSearch:totalResults>131</openSearch:totalResults>
 <openSearch:startIndex>1</openSearch:startIndex>
 <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
 <entry gd:etag="&quot;SXc5cTNQJit7I2A9XRRbGEsPQQY.&quot;">
  <id>http://www.google.com/m8/feeds/contacts/moh***ee%40gmail.com/base/15281000e768a31</id>
  <updated>2015-04-12T19:07:08.929Z</updated>
  <app:edited xmlns:app="http://www.w3.org/2007/app">2015-04-12T19:07:08.929Z</app:edited>
  <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
  <title>Sina Ghazi</title>
  <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/moh***aee%40gmail.com/15****a31" gd:etag="&quot;WR1-e34pSit7I2BlWW4TbChNHHg6LF88WhE.&quot;"/>
  <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moham****aee%40gmail.com/full/1528****8a31"/>
  <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamm***ee%40gmail.com/full/15***a31"/>
  <gd:name>
   <gd:fullName>Si***i</gd:fullName>
   <gd:givenName>Si***a</gd:givenName>
   <gd:familyName>G***zi</gd:familyName>
  </gd:name>
  <gd:email rel="http://schemas.google.com/g/2005#home" address="si***i@gmail.com" primary="true"/>
  <gContact:website href="http://www.google.com/profiles/1167****31" rel="profile"/>
 </entry>
.....

我正在使用 XPath,我可以很容易地提取 address 属性。

for item in root.findall('.//{http://schemas.google.com/g/2005}email'):
        email = item.get('address')

但是当我想获得 title 属性时,它返回 None。有什么想法吗?

【问题讨论】:

  • 显示用于提取 address 标记的代码。顺便说一句,我找不到任何地址 tag,你的意思是 attribute 吗?
  • 是的。你是对的。它是地址属性。
  • XML 中没有“title”属性。但是在两个地方有一个{http://www.w3.org/2005/Atom}title 元素。

标签: python xml xpath


【解决方案1】:

python 文档中有一个关于parsing xml with namespaces 的部分。

您可以使用 har07s 方式,效果很好,或者如果您不想多次键入整个命名空间,也可以这样做:

ns = {'ns': 'http://www.w3.org/2005/Atom'}

for element in root.findall('.//ns:title', ns):
    title = element.text

【讨论】:

  • 有没有一种方法可以遍历文件并按顺序提取每个项目?我不想提取电子邮件然后提取标题。我可以同时做吗?
  • @AminA 您可以遍历&lt;entry&gt; 节点,然后从每个节点中提取电子邮件地址和标题。
【解决方案2】:

你可以试试这个方法:

for item in root.findall('.//{http://www.w3.org/2005/Atom}title'):
    title = item.text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多