启动Zeep客户端时收到无效的XML内容
当我像这样初始化一个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)
我尝试了这里提到的解决方案,但都没有成功。我感觉我可能需要通过某个参数来改变XML解析器的工作方式,但由于我对这个库还不太熟悉,所以不太确定。
1 个回答
0
概述
#1 检查 WSDL
打开浏览器,访问这个网址
https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl
#2 获取函数列表
把 WSDL 的网址粘贴到这里,然后点击 OK
你可以看到函数列表
#3 测试函数
点击其中一个函数
然后点击 Request1
在 <cara:search>
中输入 Superman
在 <cara:year>
中输入 1978
然后点击 ▶
接着在下一个面板中点击 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
结果
<?xml version="1.0" encoding="utf-8"?><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><![CDATA[]]></ratingReason><distributor><![CDATA[Warner Bros. Inc.]]></distributor></title></SearchResults>
#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."
}