Python Zeep WSDL 意外元素回溯

Python Zeep WSDL Unexpected Elements Traceback

我无法处理下面的问题。

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

我也尝试 strict=False 设置,但 time zeep 返回为 none。

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))

谢谢。

解决方案:

首先,查看日志:

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')

我遇到了同样的问题 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