代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何附加递归API调用并将结果写入CSV?

  •  0
  • RustyShackleford  · 技术社区  · 7 年前

    我必须根据XML API响应创建csv。我有两个问题。第一,由于某种原因,我正在进行的API调用在附加后没有保存到列表中,列表完全消失。

    第二,由于我无法存储API调用的结果,我不知道如何爬过结果并创建csv。

    def xml_event_info(eventID):       
        xml ='''        
        <?xml version="1.0" encoding="UTF-8"?>
        <YourMembership>
           <Version>2.25</Version>
           <ApiKey>xxxx</ApiKey>
           <CallID>001</CallID>
           <></>
           <SaPasscode>xxxx</SaPasscode>
           <Call Method = "Sa.People.Profile.Get">
               <ID>{}</ID>
           </Call>
        </YourMembership>        
        '''        
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        r = requests.post('https://api.yourmembership.com', 
                          data=xml.format(eventID), headers=headers)        
        print(r.text)      
    
    
    xml ='''    
    <?xml version="1.0" encoding="utf-8" ?>
    <YourMembership>
        <Version>2.25</Version>
        <ApiKey>xxxx</ApiKey>
        <CallID>001</CallID>
        <SaPasscode>xxxx</SaPasscode>
        <Call Method="Sa.Groups.Group.GetMembershipLog">
            <GroupID>12345</GroupID>
            <ItemID></ItemID>
            <StartDate></StartDate>
        </Call>
    </YourMembership>
    '''        
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    r = requests.post('https://api.yourmembership.com', data=xml, headers=headers)
    print r.text
    # BUILD XML TREE OBJECT    
    tree = et.fromstring(r.text)
    
    storage = []
    # PARSE People ID TEXT AND PASS INTO FUNCTION
    for i in tree.iterfind('.//ID'):
         y = xml_event_info(i.text)
         storage.append(y)
    

    其次 y = xml_event_info(i.text) 是我需要解析为CSV的实际人员配置文件数据,这是XML的外观:

    <?xml version="1.0" encoding="utf-8" ?>
    
    <YourMembership_Response>
    <ErrCode>0</ErrCode>
    <ExtendedErrorInfo></ExtendedErrorInfo>
    <Sa.People.Profile.Get>
    <ID>098765</ID>
    <WebsiteID>1234</WebsiteID>
    <PrimaryGroupCode>abc</PrimaryGroupCode>
    <IsMember>1</IsMember>
    <IsNonMember>0</IsNonMember>
    <Registered>2017-09-07 18:48:00</Registered>
    <LastUpdated>2017-09-11 15:19:54</LastUpdated>
    <ImportID>u293248</ImportID>
    <ConstituentID></ConstituentID>
    <EmailAddr>xxxx</EmailAddr>
    <EmailBounced>0</EmailBounced>
    <NamePrefix>xxx</NamePrefix>
    <FirstName>John</FirstName>
    <MiddleName></MiddleName>
    <LastName>Smith</LastName>
    <NameSuffix></NameSuffix>
    <Nickname></Nickname>
    <Gender>M</Gender>
    <Birthdate></Birthdate>
    <MaritalStatus></MaritalStatus>
    <MaidenName></MaidenName>
    <SpouseName></SpouseName>
    <AnniversaryDate></AnniversaryDate>
    <Employer>Welk Resorts</Employer>
    <Title>xxxx</Title>
    <Profession></Profession>
    <Membership>xxxxx</Membership>
    <MembershipExpiry>2017-09-08 00:00:00</MembershipExpiry>
    <MemberTypeCode>xxxx</MemberTypeCode>
    <Approved>1</Approved>
    <Suspended>1</Suspended>
    <Username>xx@xx.com</Username>
    <PasswordHash>xxxx</PasswordHash>
    <AltEmailAddr>xxxx</AltEmailAddr>
    <HomeAddrLines></HomeAddrLines>
    <HomeCity></HomeCity>
    <HomeLocation>xxxx</HomeLocation>
    <HomePostalCode></HomePostalCode>
    <HomeCountry>United States</HomeCountry>
    <Website></Website>
    <HomePhAreaCode></HomePhAreaCode>
    <HomePhone></HomePhone>
    <MobileAreaCode></MobileAreaCode>
    <Mobile></Mobile>
    <EmpAddrLines></EmpAddrLines>
    <EmpCity></EmpCity>
    <EmpLocation>Alabama</EmpLocation>
    <EmpPostalCode></EmpPostalCode>
    <EmpCountry>United States</EmpCountry>
    <BusinessWebsite></BusinessWebsite>
    <EmpPhAreaCode></EmpPhAreaCode>
    <EmpPhone></EmpPhone>
    <EmpFaxAreaCode></EmpFaxAreaCode>
    <EmpFax></EmpFax>
    <HeadshotImageURI></HeadshotImageURI>
    <LastRenewalReminderSent></LastRenewalReminderSent>
    <GamificationPoints></GamificationPoints>
    <MasterID></MasterID>
    <LastRenewalDate>2017-09-07 00:00:00</LastRenewalDate>
    <ApprovalDate>9/11/2017 00:00:00 PM</ApprovalDate>
    <LastModifiedDate>2017-09-11 00:00:00 </LastModifiedDate>
    <QueuedForDelete>0</QueuedForDelete>
    <QueuedForDeleteDate></QueuedForDeleteDate>
    <Latitude>0</Latitude>
    <Longitude>0</Longitude>
    <MembershipExpires>1</MembershipExpires>
    <MembershipEffectiveExpiresDate>2017-09-08 00:00:00</MembershipEffectiveExpiresDate>
    <CustomFieldResponses>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Member Referral</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Individual</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ReferredBy" Visibility="">
    <Values>
    <Value>xxxx</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Practitioner</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>xxxxx</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ApprovalDate" Visibility="">
    <Values>
    <Value>9.8.17</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ChapterApproval" Visibility="">
    <Values>
    <Value>declined 9.11.17</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="whois" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="HelpingHandsSummary" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="APCProspect" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="formerchapter" Visibility="">
    <Values>
    <Value>San Diego</Value>
    </Values>
    </CustomFieldResponse>
    </CustomFieldResponses>
    </Sa.People.Profile.Get>
    </YourMembership_Response>
    
    <?xml version="1.0" encoding="utf-8" ?>
    
    <YourMembership_Response>
    <ErrCode>0</ErrCode>
    <ExtendedErrorInfo></ExtendedErrorInfo>
    <Sa.People.Profile.Get>
    <ID>098765</ID>
    <WebsiteID>1234</WebsiteID>
    <PrimaryGroupCode>abc</PrimaryGroupCode>
    <IsMember>1</IsMember>
    <IsNonMember>0</IsNonMember>
    <Registered>2017-09-07 18:48:00</Registered>
    <LastUpdated>2017-09-11 15:19:54</LastUpdated>
    <ImportID>u293248</ImportID>
    <ConstituentID></ConstituentID>
    <EmailAddr>xxxx</EmailAddr>
    <EmailBounced>0</EmailBounced>
    <NamePrefix>xxx</NamePrefix>
    <FirstName>John</FirstName>
    <MiddleName></MiddleName>
    <LastName>Smith</LastName>
    <NameSuffix></NameSuffix>
    <Nickname></Nickname>
    <Gender>M</Gender>
    <Birthdate></Birthdate>
    <MaritalStatus></MaritalStatus>
    <MaidenName></MaidenName>
    <SpouseName></SpouseName>
    <AnniversaryDate></AnniversaryDate>
    <Employer>Welk Resorts</Employer>
    <Title>xxxx</Title>
    <Profession></Profession>
    <Membership>xxxxx</Membership>
    <MembershipExpiry>2017-09-08 00:00:00</MembershipExpiry>
    <MemberTypeCode>xxxx</MemberTypeCode>
    <Approved>1</Approved>
    <Suspended>1</Suspended>
    <Username>xx@xx.com</Username>
    <PasswordHash>xxxx</PasswordHash>
    <AltEmailAddr>xxxx</AltEmailAddr>
    <HomeAddrLines></HomeAddrLines>
    <HomeCity></HomeCity>
    <HomeLocation>xxxx</HomeLocation>
    <HomePostalCode></HomePostalCode>
    <HomeCountry>United States</HomeCountry>
    <Website></Website>
    <HomePhAreaCode></HomePhAreaCode>
    <HomePhone></HomePhone>
    <MobileAreaCode></MobileAreaCode>
    <Mobile></Mobile>
    <EmpAddrLines></EmpAddrLines>
    <EmpCity></EmpCity>
    <EmpLocation>Alabama</EmpLocation>
    <EmpPostalCode></EmpPostalCode>
    <EmpCountry>United States</EmpCountry>
    <BusinessWebsite></BusinessWebsite>
    <EmpPhAreaCode></EmpPhAreaCode>
    <EmpPhone></EmpPhone>
    <EmpFaxAreaCode></EmpFaxAreaCode>
    <EmpFax></EmpFax>
    <HeadshotImageURI></HeadshotImageURI>
    <LastRenewalReminderSent></LastRenewalReminderSent>
    <GamificationPoints></GamificationPoints>
    <MasterID></MasterID>
    <LastRenewalDate>2017-09-07 00:00:00</LastRenewalDate>
    <ApprovalDate>9/11/2017 00:00:00 PM</ApprovalDate>
    <LastModifiedDate>2017-09-11 00:00:00 </LastModifiedDate>
    <QueuedForDelete>0</QueuedForDelete>
    <QueuedForDeleteDate></QueuedForDeleteDate>
    <Latitude>0</Latitude>
    <Longitude>0</Longitude>
    <MembershipExpires>1</MembershipExpires>
    <MembershipEffectiveExpiresDate>2017-09-08 00:00:00</MembershipEffectiveExpiresDate>
    <CustomFieldResponses>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Member Referral</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Individual</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ReferredBy" Visibility="">
    <Values>
    <Value>xxxx</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>Practitioner</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values>
    <Value>xxxxx</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="xxxx" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ApprovalDate" Visibility="">
    <Values>
    <Value>9.8.17</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="ChapterApproval" Visibility="">
    <Values>
    <Value>declined 9.11.17</Value>
    </Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="whois" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="HelpingHandsSummary" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="APCProspect" Visibility="">
    <Values></Values>
    </CustomFieldResponse>
    <CustomFieldResponse FieldCode="formerchapter" Visibility="">
    <Values>
    <Value>San Diego</Value>
    </Values>
    </CustomFieldResponse>
    </CustomFieldResponses>
    </Sa.People.Profile.Get>
    </YourMembership_Response>
    

    有没有其他方法可以让每个xml标记快速成为csv中的一列?如果有,我不介意手动键入要写入的字段。但希望保存结果并将其拆分为csv。CSV应如下所示:

    ID     FirstName   LastName EmailAddr
    12312   John        Smith    john@smith.com
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Bill Bell    7 年前

    这段代码获取输入xml文件中每个配置文件中“children”的标记和文本。但是 小心 ,这段代码不会试图编译出现在所有配置文件中的所有标记的完整集。如果某些配置文件具有其他配置文件没有的标记,那么这将导致编写csv文件的复杂化,因为在开始编写csv文件之前,您需要知道csv中有多少列。

    def xml_event_info(eventID):       
        xml ='''        
        <?xml version="1.0" encoding="UTF-8"?>
        <YourMembership>
           <Version>2.25</Version>
           <ApiKey>xxxx</ApiKey>
           <CallID>001</CallID>
           <></>
           <SaPasscode>xxxx</SaPasscode>
           <Call Method = "Sa.People.Profile.Get">
               <ID>{}</ID>
           </Call>
        </YourMembership>        
        '''        
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        r = requests.post('https://api.yourmembership.com', 
                          data=xml.format(eventID), headers=headers)        
        print(r.text)      
    
    
    xml ='''    
    <?xml version="1.0" encoding="utf-8" ?>
    <YourMembership>
        <Version>2.25</Version>
        <ApiKey>xxxx</ApiKey>
        <CallID>001</CallID>
        <SaPasscode>xxxx</SaPasscode>
        <Call Method="Sa.Groups.Group.GetMembershipLog">
            <GroupID>12345</GroupID>
            <ItemID></ItemID>
            <StartDate></StartDate>
        </Call>
    </YourMembership>
    '''        
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    r = requests.post('https://api.yourmembership.com', data=xml, headers=headers)
    
    from xml.etree import et
    tree = et.fromstring(r.text)
    people_profiles = tree.findall('.//Sa.People.Profile.Get')
    for people_profile in people_profiles:
        'New profile'
        for c, child in enumerate(people_profile.getchildren()):
            print (child.tag, child.text)