解析Google联系人以Python格式提供

时间:2015-09-01 14:03:27

标签: python xml google-contacts

我正在尝试使用python并基于Retrieve all contacts from gmail using python获取Google联系人Feed中每个联系人的姓名。

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)

我能够获得联系人n的数量,但没有获得fullName的运气。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

如果有人需要,我找到了从Google通讯录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)