【问题标题】:Python Zeep WSDL Unexpected Elements TracebackPython Zeep WSDL 意外元素回溯
【发布时间】:2019-12-24 12:03:15
【问题描述】:

我无法在下面处理这个。

XMLParseError: Unexpected element 'metadata', expected 'id' error.

我也尝试了strict=False 设置,但当时 zeep 没有返回。

from zeep import Client
import zeep
wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'

cl = Client(wsdl=wsdl)
request_data = {"auth":{'appKey' : ***,
'appSecret' : **},'categoryId':int(1003221),"pagingData":{'pageSize':1,'currentPage':0}}
print(cl.service.GetCategoryAttributes(**request_data))

谢谢。

【问题讨论】:

    标签: python-3.x api soap wsdl zeep


    【解决方案1】:

    解决方案:

    首先,检查日志:

    import logging.config
    logging.config.dictConfig({
        'version': 1,
        'formatters': {
            'verbose': {
                'format': '%(name)s: %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose',
            },
        },
        'loggers': {
            'zeep.transports': {
                'level': 'DEBUG',
                'propagate': True,
                'handlers': ['console'],
            },
        }
    })
    

    第二:

    from zeep import Client
    from zeep.plugins import HistoryPlugin
    wsdl = 'https://api.n11.com/ws/CategoryService.wsdl'
    from zeep import Client, Settings
    settings = Settings(strict=False, xml_huge_tree=True)
    history = HistoryPlugin()
    from zeep.transports import Transport
    transport = Transport(timeout=10)
    key={'appKey' : '**',
    'appSecret' : '**',
    }
    cat= {'categoryId':1003221}
    dat= {'pageSize':1,'currentPage':1}
    client = Client(wsdl=wsdl, transport=transport, plugins=[history], settings=settings)
    with client.settings(raw_response=False,strict=False, xml_huge_tree=True):
        response = client.service.GetCategoryAttributes(key,1003221,pagingData=dat)
        #assert response.status_code == 200
        #assert response.content
    #node = client.create_message(client.service, 'GetCategoryAttributes',key,1003221,pagingData=dat)
    from lxml import etree
    
    try:
        for hist in [history.last_sent, history.last_received]:
            print(etree.tostring(hist["envelope"], encoding="unicode", pretty_print=True))   
    except (IndexError, TypeError):
        # catch cases where it fails before being put on the wire
        pass
    

    最后,您可以使用 bs4 解析 xml 内容:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(ass, 'lxml')
    soup.find('category').find('metadata')
    

    【讨论】:

      【解决方案2】:

      我在使用相同的 API 时遇到了同样的问题。我的解决方案是更改 zeep 的设置。 你应该使用xsd_ignore_sequence_order=True。 我希望现在还不算太晚。

      settings = Settings(strict=False, xml_huge_tree=True, xsd_ignore_sequence_order=True)
      

      阅读更多: https://docs.python-zeep.org/en/master/settings.html#module-zeep.settings

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-05
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多