在 python 中使用 zeep 从 wsdl 文件检索数据

retrive data from wsdl file using zeep in python

我可以访问 wsdl 文件,我需要得到一份药物清单作为回应

但我受困于此,这里是我的尝试


from zeep import Client
from requests.auth import HTTPBasicAuth  
from requests import Session
from zeep.transports import Transport

username = 'username'
password = 'password'
wsdl = 'wsdl link'

session = Session()
session.auth = HTTPBasicAuth(username, password)
cl = Client(wsdl,transport=Transport(session=session))
r = cl.service.getDrugList('DRUGSTATUS')
print(r['DRUGSTATUS'])

它给我这个错误

zeep.exceptions.Fault: 80210

当我从

中删除参数时
r = cl.service.getDrugList()

它给我这个错误

raise exceptions.ValidationError(
zeep.exceptions.ValidationError: Missing element DRUGSTATUS (DrugListServiceRequest.DRUGSTATUS)

更新

--

@Tarique 先生请求后

根据这个link https://github.com/mvantellingen/python-zeep/issues/297#issuecomment-271803117 我使用相同的命令并为 mzssp wsdl linke 提供相同的输出,因为我发现 auth 有问题,这里是结果


Prefixes:
    xsd: http://www.w3.org/2001/XMLSchema
    ns0: http://org linke/DrugListService

Global elements:
    ns0:DrugListServiceRequest(ns0:drugListServiceRequest)
    ns0:DrugListServiceRequestType(ns0:drugListServiceRequest)
    ns0:DrugListServiceResponse(ns0:drugListServiceResponse)
    ns0:DrugListServiceResponseType(ns0:drugListServiceResponse)
    ns0:ServiceError(ns0:faultBean)
    ns0:drug(ns0:drug)
    ns0:supplier(ns0:supplier)


Global types:
    xsd:anyType
    ns0:drug(GTIN: xsd:string, DRUGNAME: xsd:string, DOMAINID: xsd:short, LEGALSTATUS: xsd:short, MARKETINGSTATUS: xsd:short, DRUGSTATUS: xsd:short, SUPPLIERLIST: {SUPPLIER: ns0:supplier[]}, ISIMPORTABLE: xsd:short, ISEXPORTABLE: xsd:short, REGISTRATIONNUMBER: xsd:string, GENERICNAME: xsd:string, PRICE: xsd:decimal, DOSAGEFORM: xsd:string, PACKAGESIZE: xsd:string, PACKAGETYPE: xsd:string, STRENGTHVALUE: xsd:string, STRENGTHVALUEUNIT: xsd:string, VOLUME: xsd:string, UNITOFVOLUME: xsd:string)
    ns0:drugListServiceRequest(DRUGSTATUS: xsd:string)
    ns0:drugListServiceResponse(DRUGLIST: {DRUG: ns0:drug[]})
    ns0:faultBean(FC: xsd:string)
    ns0:supplier(GLN: xsd:string, SUPPLIERNAME: xsd:string)
    xsd:ENTITIES
    xsd:ENTITY
    xsd:ID
    xsd:IDREF
    xsd:IDREFS
    xsd:NCName
    xsd:NMTOKEN
    xsd:NMTOKENS
    xsd:NOTATION
    xsd:Name
    xsd:QName
    xsd:anySimpleType
    xsd:anyURI
    xsd:base64Binary
    xsd:boolean
    xsd:byte
    xsd:date
    xsd:dateTime
    xsd:decimal
    xsd:double
    xsd:duration
    xsd:float
    xsd:gDay
    xsd:gMonth
    xsd:gMonthDay
    xsd:gYear
    xsd:gYearMonth
    xsd:hexBinary
    xsd:int
    xsd:integer
    xsd:language
    xsd:long
    xsd:negativeInteger
    xsd:nonNegativeInteger
    xsd:nonPositiveInteger
    xsd:normalizedString
    xsd:positiveInteger
    xsd:short
    xsd:string
    xsd:time
    xsd:token
    xsd:unsignedByte
    xsd:unsignedInt
    xsd:unsignedLong
    xsd:unsignedShort

Bindings:
    Soap11Binding: {http://org linke/DrugListService}DrugListServiceBinding

Service: DrugListService
    Port: DrugListService (Soap11Binding: {http://org linke/DrugListService}DrugListServiceBinding)
        Operations:
           getDrugList(DRUGSTATUS: xsd:string) -> DRUGLIST: {DRUG: ns0:drug[]}

None



感谢请求的输出。

根据定义:getDrugList(DRUGSTATUS: xsd:string) -> DRUGLIST: {DRUG: ns0:drug[]} 看起来方法 getDrugList 只接受 1 个字符串类型的参数,它 returns 一个包含 drug 项的列表。

您正在传递一个参数 'DRUGSTATUS',我不确定该参数是否为服务可接受的有效状态:

r = cl.service.getDrugList('DRUGSTATUS')

你应该这样调用方法:

r = cl.service.getDrugList(DRUGSTATUS='some_status')
# where some_status is valid DRUGSTATUS string. It could be found in service docs or examples.

# then check the response status & content:
print(r.status_code)
print(r.content)

此外,如果 response 是有效响应,则可以使用 for 循环打印 DRUGLIST

希望这对您有所帮助