屬性我使用beautifulstonesoup和python解析來自谷歌的XML飼料,它的偉大工程。我也在創建一個csv並將其上傳到Google Docs,這也很好。問題是當我在xml中遇到空的文本屬性時,解析器停止。現在不是問題,因爲所有的屬性都有數據,但是第一次沒有,它會中斷。越過NoneType在BeautifulSoup
代碼:
import atom
import gdata.auth
import gdata.contacts
import gdata.contacts.client
import gdata.docs.service
import gdata.docs.data
from BeautifulSoup import BeautifulStoneSoup as Soup
import csv
email = '[email protected]'
password = 'password'
domain = 'domain.com'
ms_client = gdata.docs.service.DocsService()
gd_client = gdata.contacts.client.ContactsClient(domain=domain)
gd_client.ClientLogin(email, password, 'profileFeedAPI')
ms_client.ClientLogin(email, password, 'peopleCSVupload')
profiles_feed = gd_client.GetProfilesFeed('https://www.google.com/m8/feeds/profiles/domain/domain.com/full?max-results=300')
soup = Soup(str(profiles_feed), selfClosingTags=['ns0:category','ns3:status', 'ns0:link','ns1:email'])
a = soup.findAll('ns0:entry')
f = open('C:\\people.csv', 'wb')
writer = csv.writer(f, quoting=csv.QUOTE_NONE, escapechar =' ')
for entry in a:
writer.writerow([entry.find('ns1:familyname').text + ',' + entry.find('ns1:givenname').text + ',' + entry.find('ns1:fullname').text + ',' + entry.find('ns1:orgtitle').text + ',' + entry.find('ns1:orgdepartment').text + ',' + entry.find('ns1:orgname').text + ',' + entry.find('ns1:email',primary=True)['address']])
f.close()
ms = gdata.data.MediaSource(file_path="C:\\people.csv", content_type=gdata.docs.service.SUPPORTED_FILETYPES['CSV'])
csv_entry = ms_client.Upload(ms, "People File")
我知道我可以做到這一點:
for entry in a:
if entry.find('ns1:orgtitle') != None:
print entry.find('ns1:orgtitle').text
elif entry.find('ns1:orgtitle') == None:
print('')
if entry.find('ns1:familyname') != None:
print entry.find('ns1:familyname').text
elif entry.find('ns1:familyname') == None:
print('')
etc...
但它是非常長的,我不知道如何集中的數據出現在一排。任何幫助,非常感謝。
拜爾抱歉在這個看着這麼晚了,我是去度週末。這是一個很棒的解決方案,但我只有一個問題。最後一項,電子郵件地址位於屬性中,而不是文本字段。我將如何能夠提取這些數據? – Kevin 2012-02-13 16:19:50
沒關係,我剛宣佈被entry.find一個變量t(「NS1:電子郵件」,初級= TRUE)[「地址」],並沒有在s.append(t)中用於在一個塊條目,再次感謝這個。 Kevin,歡迎來到 – Kevin 2012-02-13 16:39:54
。 – 2012-02-13 19:47:03