从XML python中检索数据

时间:2012-01-25 18:10:53

标签: python xml

我正试图遍历Google XML以检索大约6个字段。我使用Google提供的gdata为我的Google Apps域中的用户个人资料提取XML Feed。这是结果:

<?xml version="1.0"?>
-<ns0:feed ns1:etag="W/"LIESANDCRAPfyt7I2A9WhHERE."" xmlns:ns4="http://www.w3.org/2007/app" xmlns:ns3="http://schemas.google.com/contact/2008" xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:ns1="http://schemas.google.com/g/2005" xmlns:ns0="http://www.w3.org/2005/Atom">
    <ns0:updated>2012-01-25T14:52:12.867Z</ns0:updated>
    <ns0:category term="http://schemas.google.com/contact/2008#profile" scheme="http://schemas.google.com/g/2005#kind"/>
    <ns0:id>domain.com</ns0:id>
    <ns0:generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</ns0:generator>
    <ns0:author>
        <ns0:name>domain.com</ns0:name>
    </ns0:author>
    <ns0:link type="text/html" rel="alternate" href="http://www.google.com/"/>
    <ns0:link type="application/atom+xml" rel="http://schemas.google.com/g/2005#feed" href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full"/>
    <ns0:link type="application/atom+xml" rel="http://schemas.google.com/g/2005#batch" href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/batch"/>
    <ns0:link type="application/atom+xml" rel="self" href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full?max-results=300"/>
    <ns2:startIndex>1</ns2:startIndex>
    <ns2:itemsPerPage>300</ns2:itemsPerPage>
    <ns0:entry ns1:etag=""CRAPQR4KTit7I2A4"">
        <ns0:category term="http://schemas.google.com/contact/2008#profile" scheme="http://schemas.google.com/g/2005#kind"/>
        <ns0:id>http://www.google.com/m8/feeds/profiles/domain/domain.com/full/nperson</ns0:id>
        <ns1:name>
            <ns1:familyName>Person</ns1:familyName>
            <ns1:fullName>Name Person</ns1:fullName>
            <ns1:givenName>Name</ns1:givenName>
        </ns1:name>
        <ns0:updated>2012-01-25T14:52:13.081Z</ns0:updated>
        <ns1:organization rel="http://schemas.google.com/g/2005#work" primary="true">
            <ns1:orgTitle>JobField</ns1:orgTitle>
            <ns1:orgDepartment>DepartmentField</ns1:orgDepartment>
            <ns1:orgName>CompanyField</ns1:orgName>
        </ns1:organization>
        <ns3:status indexed="true"/>
        <ns0:title>Name Person</ns0:title>
        <ns0:link type="image/*" rel="http://schemas.google.com/contacts/2008/rel#photo" href="https://www.google.com/m8/feeds/photos/profile/domain.com/nperson"/>
        <ns0:link type="application/atom+xml" rel="self" href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/nperson"/>
        <ns0:link type="application/atom+xml" rel="edit" href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/nperson"/>
        <ns1:email rel="http://schemas.google.com/g/2005#other" address="nperson@gapps.domain.com"/>
        <ns1:email rel="http://schemas.google.com/g/2005#other" primary="true" address="nperson@domain.com"/>
        <ns4:edited>2012-01-25T14:52:13.081Z</ns4:edited>
    </ns0:entry>
    <ns0:title>domain.com's Profiles</ns0:title>
</ns0:feed>

我正在尝试使用lxml来解析数据,但它运行得不好,这是我的代码:

import atom
import gdata.auth
import gdata.contacts
import gdata.contacts.client
from lxml import etree
from lxml import objectify

email = 'admin@domain.com'
password = 'password'
domain = 'domain.com'

gd_client = gdata.contacts.client.ContactsClient(domain=domain)
gd_client.ClientLogin(email, password, 'profileFeedAPI')

profiles_feed = gd_client.GetProfilesFeed('https://www.google.com/m8/feeds/profiles/domain/domain.com/full?max-results=300')

def PrintFeed(feed):
  for i, entry in enumerate(feed.entry):
    print '\n%s %s' % (i+1, entry.title.text)

print(profiles_feed)
PrintFeed(profiles_feed)

profiles_feed2=(str(profiles_feed))

root = objectify.fromstring(profiles_feed2)

print root

print root.tag
print root.text

for e in root.entry():
    print e.tag
    print e.text

我可以让它返回饲料然后进入,但我无法探索更远。我需要的是ns1名称中的名称字段和ns1组织中的组织字段的文本格式。我有点失落,所以非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我总是建议使用BeautifulSoup,因为它易于学习API:

from BeautifulSoup import BeautifulStoneSoup as Soup

soup = Soup(open(filename))
for tag in soup.findAll('ns1:name'):
    print tag.find('ns1:familyname').text
    print tag.find('ns1:fullname').text
    print tag.find('ns1:givenname').text
for tag in soup.findAll('ns1:organization'):
    print tag.find('ns1:orgtitle').text
    print tag.find('ns1:orgdepartment').text
    print tag.find('ns1:orgname').text

示例输出:

Person
Name Person
Name
JobField
DepartmentField
CompanyField

答案 1 :(得分:1)

您可以尝试将Xpath Expressions与lxml一起使用。这肯定会让您的工作变得轻松。

例如,如果你的xml文件是:

<document>
        <name>
                <familyName>Person</familyName>
                <fullName>Name Person</fullName>
                <givenName>Name</givenName>
        </name>
</document>

比以下代码

>>> import lxml
>>> from lxml import etree
>>> et = etree.parse("test.xml")
>>> value = et.xpath("/document/name/*/text()")
>>> value
['Person', 'Name Person', 'Name']

对于xpath使用firefox的firebug插件。