启动 Zeep 客户端时收到无效的 XML 内容

问题描述 投票:0回答:1

像这样初始化 Zeep 客户端时

url = 'http://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'
client = Client(wsdl = url)

我收到错误消息

XMLSyntaxError: Invalid XML content received (Opening and ending tag mismatch: p line 122 and ul, line 124, column 20)

我已经尝试过here提到的解决方案,但没有成功。我有一种感觉,我需要通过某些参数来改变 XML 解析器的工作方式,但我不确定,因为我是使用这个库的新手。

python soap zeep
1个回答
0
投票

概述

#1 检查 WSDL

打开浏览器访问此网址

https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl

应显示XML内容

#2 获取函数列表

SoapUI 开源

将 WSDL URL 粘贴到此处,然后按 OK

您可以获取功能列表

#3 测试功能

点击其中一项功能

然后点击请求1

Superman
对于
<cara:search>

1978
对于
<cara:year>

然后按

然后单击下一个面板中的

XML
选项卡 这是函数调用的结果

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetExportWithTitleYearResponse xmlns="http://cara.org/">
         <GetExportWithTitleYearResult><![CDATA[<?xml version="1.0" encoding="utf-8"?><SearchResults><title><name><![CDATA[Superman]]]]>><![CDATA[</name><year><![CDATA[1978]]]]>><![CDATA[</year><url><![CDATA[http://www.imdb.com/find?q=Superman]]]]>><![CDATA[</url><rating><![CDATA[PG]]]]>><![CDATA[</rating><ratingReason><![CDATA[]]]]>><![CDATA[</ratingReason><distributor><![CDATA[Warner Bros. Inc.]]]]>><![CDATA[</distributor></title></SearchResults>]]></GetExportWithTitleYearResult>
      </GetExportWithTitleYearResponse>
   </soap:Body>
</soap:Envelope>

#4 测试 zeep

zeep
具有相同功能 另存为
test-function.py
文件名

from zeep import Client

# URL of the SOAP service
url = 'https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'

client = Client(url)

# Call the GetExportWithTitleYear method
response = client.service.GetExportWithTitleYear(search='Superman', year='1978')

print(response) # Print the XML data

您应该在运行之前安装依赖项。

pip install zeep

运行它

python test-function.py

结果

#5 数据转换

将打印 XML 并提取键/值作为 JSON 格式数据。

from zeep import Client
import xml.dom.minidom
import xml.etree.ElementTree as ET
import json

# URL of the SOAP service
url = 'https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'

client = Client(url)

# Call the GetExportWithTitleYear method
response = client.service.GetExportWithTitleYear(search='Superman', year='1978')

dom = xml.dom.minidom.parseString(response) # Parse the XML string

pretty_xml = dom.toprettyxml()

print(pretty_xml) # Print the XML data

root = ET.fromstring(response)

data = {}

# Loop through child elements of the root element
for child in root:
    for item in child:
        tag = item.tag
        text = item.text.strip() if item.text else ""
        data[tag] = text

json_data = json.dumps(data, indent=4)

print(json_data) # Print the JSON data

结果

<?xml version="1.0" ?>
<SearchResults>
        <title>
                <name><![CDATA[Superman]]></name>
                <year><![CDATA[1978]]></year>
                <url><![CDATA[http://www.imdb.com/find?q=Superman]]></url>
                <rating><![CDATA[PG]]></rating>
                <ratingReason/>
                <distributor><![CDATA[Warner Bros. Inc.]]></distributor>
        </title>
</SearchResults>

{
    "name": "Superman",
    "year": "1978",
    "url": "http://www.imdb.com/find?q=Superman",
    "rating": "PG",
    "ratingReason": "",
    "distributor": "Warner Bros. Inc."
}

© www.soinside.com 2019 - 2024. All rights reserved.