Search code examples
pythonxmlgoogle-contacts-api

Parsing Google contacts feed in Python


I'm trying to obtain the name of every contact in Google contact feed using python and based on Retrieve all contacts from gmail using python.

social = request.user.social_auth.get(provider='google-oauth2')
url = 'https://www.google.com/m8/feeds/contacts/default/full' + '?access_token=' + social.tokens + '&max-results=10000'
req = urllib2.Request(url, headers={'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30"})
contacts = urllib2.urlopen(req).read()  
contacts_xml = etree.fromstring(contacts)
contacts_list = []
n = 0
for entry in contacts_xml.findall('{http://www.w3.org/2005/Atom}entry'):
    n = n + 1
    for name in entry.findall('{http://schemas.google.com/g/2005}name'):
        fullName = name.attrib.get('fullName')
        contacts_list.append(fullName)

I'm able to obtain the number of contacts n, but no luck obtaining the fullName. Any help is appreciated!


Solution

  • In case someone needs it, I found the solution to obtain the name from Google Contacts feed:

    for entry in contacts_xml.findall('{http://www.w3.org/2005/Atom}entry'):
        for title in entry.findall('{http://www.w3.org/2005/Atom}title'):
            name = title.text
            contacts_list.append(name)