2012-01-25 6 views
5

से डेटा पुनर्प्राप्त करें मैं लगभग 6 फ़ील्ड पुनर्प्राप्त करने के लिए Google एक्सएमएल को पार करने की कोशिश कर रहा हूं। मैं अपने Google Apps डोमेन में उपयोगकर्ता प्रोफ़ाइल के लिए XML फ़ीड खींचने के लिए Google द्वारा प्रदान किए गए gdata का उपयोग कर रहा हूं। यह परिणाम है:एक्सएमएल पायथन

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

email = '[email protected]' 
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 

मैं इस वापस करने के लिए प्राप्त कर सकते हैं:

<?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="[email protected]"/> 
     <ns1:email rel="http://schemas.google.com/g/2005#other" primary="true" address="[email protected]"/> 
     <ns4:edited>2012-01-25T14:52:13.081Z</ns4:edited> 
    </ns0:entry> 
    <ns0:title>domain.com's Profiles</ns0:title> 
</ns0:feed> 

मैं डेटा पार्स करने के लिए lxml उपयोग करने के लिए कोशिश कर रहा हूँ, लेकिन यह इतनी अच्छी तरह से बाहर काम नहीं कर रहा, यह मेरा कोड है फ़ीड और फिर प्रवेश, लेकिन मैं किसी भी आगे का पता नहीं लगा सकता। एएलएल मुझे एनएस 1 नाम में नाम फ़ील्ड और ns1 संगठन में संगठन फ़ील्ड की आवश्यकता है। मैं थोड़ा हारा हूं, इसलिए किसी भी मदद की सराहना की जाती है।

+0

"थोड़ा खो गया" होने से परे आपको क्या त्रुटि/समस्या का सामना करना पड़ रहा है? इसके अलावा, यदि आपको केवल कुछ फ़ील्ड की आवश्यकता है तो इसके लिए SAX पार्सिंग पर विचार करें। – Marcin

+0

@marcin जिस समस्या का सामना कर रहा हूं वह यह है कि मुझे आवश्यक फ़ील्ड नहीं मिल सकते हैं, और मुझे नहीं पता कि यह कैसे करना है। – Kevin

+0

इस प्रश्न को फिर से खोलने के लिए वोट दिया गया। मूल समस्या यह है कि आप नामस्थानों से अवगत नहीं हैं। देखें [lxml objectify के साथ नामस्थानों का उपयोग करके] (http://lxml.de/objectify.html#namespace-handling), या वही करें [xpath का उपयोग करके] (http://lxml.de/xpathxslt.html#xpath) –

उत्तर

2

मैं हमेशा अपनी आसान जानने के लिए एपीआई की वजह से BeautifulSoup उपयोग करने के लिए सलाह देते हैं:

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 
+2

यदि आप एक्सएमएल से काम कर रहे हैं तो कम से कम 'सुंदरस्टोन सूप' का उपयोग करें ... – ThiefMaster

+0

@ थिफमास्टर उत्तर 'सुंदरस्टोन सूप' का उपयोग करने के लिए अपडेट किया गया। आपके सुझाव के लिए बहुत बहुत धन्यवाद। – jcollado

+0

@jcollado धन्यवाद यह बहुत अच्छा काम करता है – Kevin

1

आप lxml.It साथ Xpath Expressions का उपयोग कर निश्चित रूप से अपना काम आसानी होगी कोशिश कर सकते हैं ।

उदाहरण के लिए यदि अपने 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 के फ़ायरबग ऐड-ऑन का उपयोग से।

संबंधित मुद्दे