如何处理API响应

时间:2017-03-29 18:45:42

标签: python python-3.x api parsing xml-parsing

我能够连接到Mind Body api并运行一个简单的命令来获取所有客户端数据

from Helper.ClientService import ClientServiceCalls

calls = ClientServiceCalls()
clients = calls.GetAllClients()
print(clients)

服务器将回复这些信息:

(GetClientsResult){
   Status = "Success"
   ErrorCode = 200
   XMLDetail = "Full"
   ResultCount = 4503
   CurrentPageIndex = 0
   TotalPageCount = 181
   Clients = 
      (ArrayOfClient){
         Client[] = 
            (Client){
               MobileProvider = None
               AppointmentGenderPreference = "None"
               Gender = "Female"
               IsCompany = False
               LiabilityRelease = False
               PromotionalEmailOptIn = True
               CreationDate = 2017-02-23 00:00:00
               Liability = 
                  (Liability){
                     IsReleased = False
                     AgreementDate = None
                     ReleasedBy = None
                  }
               UniqueID = 100015484
               ID = "100015484"
               FirstName = "Sdfoij"
               LastName = "[asodfj"
               EmailOptIn = True
               State = "CA"
               Country = "US"
               BirthDate = None
               FirstAppointmentDate = 2017-03-03 00:00:00
               HomeLocation = 
                  (Location){
                     SiteID = -99
                     BusinessDescription = ""The MINDBODY Health Club Demo is awesome." - Anonymous (but probably someone cool and smart)"
                     AdditionalImageURLs = ""
                     FacilitySquareFeet = None
                     TreatmentRooms = None
                     HasClasses = True
                     PhoneExtension = None
                     ID = 1
                     Name = "Clubville"
                  }
               PhotoURL = "https://clients.mindbodyonline.com/studios/DemoAPISandboxRestore/clients/100015484_large.jpg?v=98"
               IsProspect = False
               Status = "Active"
               ContactMethod = 1
            }.... **and continue printing other client informations**

     }
 }

现在问题是我想从中提取这些信息,

Client Email, Client Name, Client Phone Number, Client Status (active or inactive), Client Birthday, Client Address, Most Recent Visit Date, Most Recent Visit Description, Start Date, Custom Field(s)

但是我不知道我可以用什么库来解析这个输出,我在想美丽的汤,但我不太确定,

我真的是与apis合作的新手,所以如果有人能给我一个关于如何做到这一点的想法,那真的很棒。

1 个答案:

答案 0 :(得分:3)

好的,我在googs中搜索了mindbody WSDL并找回了以下链接:https://api.mindbodyonline.com/0_5/SiteService.asmx?wsdl。从查看API示例中的代码看起来他们肯定使用SOAP。我的建议是你尝试以下方法:

from suds.client import Client
from Helper.ClientService import ClientServiceMethods
calls = ClientServiceMethods()
clients = calls.GetAllClients()
client_dict = Client.dict(clients)

或者看看这个link做出回应并将其变成一个词典。