如何在Python(Zeep模块)中修复“'str'对象没有属性'keys'”?

时间:2019-08-30 16:31:46

标签: python soap zeep

更新05-09-2019 :我仍然无法解决此问题,但我至少想出了问题的根源。显然Zeep无法确定某些元素的类型,我想这就是导致我最初描述的错误的原因:

DEBUG:zeep.xsd.schema:register_element('{http://eur-lex.europa.eu/search}WORK_PART_OF_WORK', <Element(name='WORK_PART_OF_WORK', type=<UnresolvedType(qname='{http://eur-lex.europa.eu/search}Relation')>)>)

这发生在多个元素上,所以我现在的问题是如何处理这些情况。有没有一种方法可以手动定义无法识别的元素的类型?

原始帖子:

我正在使用Python 3.6和Flask开发Web应用程序,在这里我需要使用SOAP API从Web服务获取一些数据。我已经安装了Zeep模块来执行此操作,但是由于这是我第一次使用SOAP API,因此Zeep非常迷路。 据我所知,请求已正确发送,并且正在终端窗口中从Web服务接收信息,但是随后出现以下错误:

  

AttributeError:'str'对象没有属性'keys'

以下是我需要使用的WSDL:https://eur-lex.europa.eu/eurlex-ws?wsdl和我要运行的XML查询,我已经在SOAPUI中对其进行了测试,并且效果很好:

   <soap:Body>
      <sear:searchRequest>
         <sear:expertQuery>Title~"Decision (EU) 2016/342"</sear:expertQuery>
         <sear:page>1</sear:page>
         <sear:pageSize>10</sear:pageSize>
         <sear:searchLanguage>en</sear:searchLanguage>
      </sear:searchRequest>
   </soap:Body>

我在Python中的代码是:

history = HistoryPlugin()
client = Client("https://eur-lex.europa.eu/eurlex-ws?wsdl", wsse=UsernameToken("#######","###########"), plugins=[history])
request_data = {
    'expertQuery': 'Title~"Decision (EU) 2016/342"',
    'page': 1,
    'pageSize': 10,
    'searchLanguage': 'en'
}
response = client.service.doQuery(**request_data)

在终端窗口中,我看到请求正在发送并且正在接收数据:

DEBUG:zeep.transports:HTTP Post to https://eur-lex.europa.eu/EURLexWebService:
<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://www.w3.org/2003/05/soap-envelope"><soap-env:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>########</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">###########</wsse:Password></wsse:UsernameToken></wsse:Security></soap-env:Header><soap-env:Body><ns0:searchRequest xmlns:ns0="http://eur-lex.europa.eu/search"><ns0:expertQuery>Title~"Decision (EU) 2016/342"</ns0:expertQuery><ns0:page>1</ns0:page><ns0:pageSize>10</ns0:pageSize><ns0:searchLanguage>en</ns0:searchLanguage></ns0:searchRequest></soap-env:Body></soap-env:Envelope>
DEBUG:urllib3.connectionpool:https://eur-lex.europa.eu:443 "POST /EURLexWebService HTTP/1.1" 200 None
DEBUG:zeep.transports:HTTP Response from https://eur-lex.europa.eu/EURLexWebService (status: 200):
<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><env:Header/><S:Body><searchResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:searchLayerHelper="xalan//eu.europa.ec.op.searchlayer.domain.util.SearchLayerHelper" xmlns="http://eur-lex.europa.eu/search" xsi:schemaLocation="http://eur-lex.europa.eu/search https://eur-lex.europa.eu/eurlex-ws?xsd=3">
   <numhits>1</numhits>
   <totalhits>1</totalhits>
   <page>1</page>
   <language>en</language>
   <result>
      <reference>eng_cellar:9c53bb30-eb42-11e5-8a81-01aa75ed71a1_en</reference>
      <rank>1</rank>
      <content>
         <NOTICE>
            <EXPRESSION>
               <EXPRESSION_TITLE>
                  <VALUE>Council Decision (EU) 2016/342 of 12 February 2016 on the conclusion, on behalf of the Union, of the Stabilisation and Association Agreement between the European Union and the European Atomic Energy Community, of the one part, and Kosovo *, of the other part</VALUE>
               </EXPRESSION_TITLE>
               <EXPRESSION_USES_LANGUAGE>
                  <URI>
                     <IDENTIFIER>ENG</IDENTIFIER>
                  </URI>
               </EXPRESSION_USES_LANGUAGE>
            </EXPRESSION>
            <WORK>
               <ID_CELEX>
                  <VALUE>32016D0342</VALUE>
               </ID_CELEX>
               <RESOURCE_LEGAL_IN-FORCE>
                  <VALUE>true</VALUE>
               </RESOURCE_LEGAL_IN-FORCE>
               <RESOURCE_LEGAL_PUBLISHED_IN_OFFICIAL-JOURNAL>
                  <EMBEDDED_NOTICE>
                     <WORK>
(***SHORTENED BECAUSE THE RESULTS ARE REALLY LONG***)
                              </URI>
                           </SAMEAS>
                        </MANIFESTATION_PART_OF_MANIFESTATION>
                     </MANIFESTATION>
                  </EMBEDDED_NOTICE>
               </WORK_HAS_EXPRESSION>
            </WORK>
         </NOTICE>
      </content>
   </result>
</searchResults></S:Body></S:Envelope>

但是在我得到以下错误列表之后:

127.0.0.1 - - [30/Aug/2019 16:59:31] "GET /test HTTP/1.1" 500 -
INFO:werkzeug:127.0.0.1 - - [30/Aug/2019 16:59:31] "GET /test HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/luiscosta/.local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/luiscosta/eur-lex/helpers.py", line 38, in decorated_function
    return f(*args, **kwargs)
  File "/home/luiscosta/eur-lex/application.py", line 128, in test
    response = client.service.doQuery(**request_data)
  File "/usr/local/lib/python3.6/dist-packages/zeep/proxy.py", line 45, in __call__
    kwargs,
  File "/usr/local/lib/python3.6/dist-packages/zeep/wsdl/bindings/soap.py", line 130, in send
    return self.process_reply(client, operation_obj, response)
  File "/usr/local/lib/python3.6/dist-packages/zeep/wsdl/bindings/soap.py", line 197, in process_reply
    result = operation.process_reply(doc)
  File "/usr/local/lib/python3.6/dist-packages/zeep/wsdl/bindings/soap.py", line 396, in process_reply
    return self.output.deserialize(envelope)
  File "/usr/local/lib/python3.6/dist-packages/zeep/wsdl/messages/soap.py", line 94, in deserialize
    body_result = self._deserialize_body(body)
  File "/usr/local/lib/python3.6/dist-packages/zeep/wsdl/messages/soap.py", line 425, in _deserialize_body
    result = self.body.parse(xmlelement, self.wsdl.types, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 609, in parse_xmlelements
    xmlelements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 609, in parse_xmlelements
    xmlelements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 609, in parse_xmlelements
    xmlelements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 362, in parse_xmlelements
    context=context,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 609, in parse_xmlelements
    xmlelements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 609, in parse_xmlelements
    xmlelements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 194, in parse_xmlelement
    elements, schema, name, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 307, in parse_xmlelements
    sub_elements, schema, context=context
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 176, in parse_xmlelements
    item = self.parse(xmlelement, schema, allow_none=True, context=context)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/element.py", line 130, in parse
    schema_type=self.type,
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/types/complex.py", line 219, in parse_xmlelement
    value = self._value_class(**init_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/valueobjects.py", line 95, in __init__
    items = _process_signature(self._xsd_type, args, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/valueobjects.py", line 207, in _process_signature
    values = element.parse_kwargs(kwargs, element_name, available_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 181, in parse_kwargs
    value = element.parse_kwargs(item_value, item_name, item_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zeep/xsd/elements/indicators.py", line 686, in parse_kwargs
    available_sub_kwargs = set(sub_kwargs.keys())
AttributeError: 'str' object has no attribute 'keys'

我完全迷路了,不知道如何解决此问题,因为我不了解该错误的含义,并且无法在Zeep中在线找到有关此特定错误的任何信息。

有什么想法吗?

0 个答案:

没有答案