我正在设置一个使用API的程序,最近我刚刚设法使该程序将请求发送到服务器并成功接收到响应,但是我无法使用该响应,可能我知道它是如何工作的?
我试图直接从python文件中从响应中检索值,并将它们放在两个不同位置的两个独立文件中,但是这两种方法都失败了。
import requests
import xml.etree.ElementTree as ET
def sendRequest():
request ="""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://website">
<soapenv:Header/>
<soapenv:Body>
<eter:test1>
<!--Optional:-->
<eter:_user>luac</eter:_user>
<!--Optional:-->
<eter:_pwd>124</eter:_pwd>
</object>
</soapenv:Body>
</soapenv:Envelope>"""
#encoded_request = request.encode('utf-8')
headers = {"Host": "172.54.21.14",
"Content-Type": "text/xml",
"Content-Length": "length"}
response = requests.post(url="http:/website\?",
headers = headers,
data = request)
print (response.text)
print (response.status_code)
with open("testSave.xml", "w") as f:
f.write(response.text)
def getValue():
tree = ET.parse('testSave.xml')
root = tree.getroot()
print (root.tag)
print (root.attrib)
for mdv in root:
print (mdv)
sendRequest()
getValue()
期望put能够显示变量及其值。
实际输出是tree = ET.parse('testSave.xml')
处的nameError
这是我尝试使用的响应xml
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetAvailableMetadeviceListResponse xmlns="http://www.etere.eu/EtereWSApi"><GetAvailableMetadeviceListResult><?xml version="1.0" encoding="utf-8"?>
<WSApiResponse>
<Function Name="MD_GetAvailableMetadeviceList">
<mdv id_metadevice="3" description="LOWRES"/><mdv id_metadevice="16" description="ETX-G"/><mdv id_metadevice="18" description="subs"/><mdv id_metadevice="23" description="ARCHIVE"/><mdv id_metadevice="32" description="System Storage"/>
</Function>
</WSApiResponse>
</GetAvailableMetadeviceListResult></GetAvailableMetadeviceListResponse></soap:Body></soap:Envelope>
答案 0 :(得分:0)
这里
import xml.etree.ElementTree as ET
xml = ''' <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetAvailableMetadeviceListResponse xmlns="http://www.etere.eu/EtereWSApi"><GetAvailableMetadeviceListResult>
<WSApiResponse>
<Function Name="MD_GetAvailableMetadeviceList">
<mdv id_metadevice="3" description="LOWRES"/>
<mdv id_metadevice="16" description="ETX-G"/>
<mdv id_metadevice="18" description="subs"/>
<mdv id_metadevice="23" description="ARCHIVE"/>
<mdv id_metadevice="32" description="System Storage"/>
</Function>
</WSApiResponse>
</GetAvailableMetadeviceListResult></GetAvailableMetadeviceListResponse></soap:Body></soap:Envelope>
'''
#
# Assuming all we care about is under <Function>
#
start_idx = xml.index('<Function')
end_idx = xml.index('</Function>')
xml = xml[start_idx:end_idx + 11]
root = ET.fromstring(xml)
mdv_list = root.findall('.//mdv')
for mdv in mdv_list:
print('{} --> {}'.format(mdv.attrib.get('id_metadevice'),mdv.attrib.get('description')))
输出
3 --> LOWRES
16 --> ETX-G
18 --> subs
23 --> ARCHIVE
32 --> System Storage
答案 1 :(得分:0)
with open("testSave.txt", "r+") as f:
f.write(response.text)
with open("testSave.txt", "r+") as f:
content= f.read()
f.seek(0)
f.truncate()
f.write(content.replace('<','<'))
with open("testSave.txt", "r+") as f:
content= f.read()
f.seek(0)
f.truncate()
f.write(content.replace('>','>'))
with open('testSave.txt', 'r') as f2:
data= f2.read()
print(data)
start_idx = data.index('<Function')
end_idx = data.index('</Function>')
data = data[start_idx:end_idx + 11]
root = ET.fromstring(data)
mdv_list = root.findall('.//mdv')
for mdv in mdv_list:
print('{} --> {}'.format(mdv.attrib.get('id_metadevice'),mdv.attrib.get('description')))
我找到了解决方案,这使我可以编写响应,更改所有的“&lt;”和'&gt;'到“ <”和“>”,然后使用@balderman提供的代码,提取所需的数据!谢谢!