如何访问 xmlns 命名空间 web 服务中的请求响应?
how to access to requests response in xmlns namespace webservices?
有一个 Web 服务 returns 类似这样的东西,我想在这个 [=28] 中访问 id =] 我应该怎么做?
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"id":"1819","responseCode":"0","responseMessage":"0"}</string>
此外,我的功能在这里:
import requests
def sen_request():
url = "somewhere.com/somewhere.asmx/new_one"
payload = {}
headers= {}
response = requests.request("GET", url, headers=headers, data = payload)
return (response)
- 我尝试使用 response.json() 访问 JSON 中的响应,但出现此错误:
Exception Type: JSONDecodeError
Exception Value: Expecting value: line 1 column 1 (char 0)
该代码使用json库和XML库解析以提取id
import json
import xml.etree.ElementTree as ET
data = '''<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"id":"1819","responseCode":"0","responseMessage":"0"}</string>'''
data1 = '''<?xml version="1.0" encoding="utf-8"?><string xmlns="tempuri.org">{"id":{"id1":"1819"},"responseCode":"0","responseMessage":"0"}</string>'''
root = ET.fromstring(data)
print(json.loads(root.text)['id'])
root = ET.fromstring(data1)
print(json.loads(root.text)['id']['id1'])
输出
1819
1819
有一个 Web 服务 returns 类似这样的东西,我想在这个 [=28] 中访问 id =] 我应该怎么做?
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"id":"1819","responseCode":"0","responseMessage":"0"}</string>
此外,我的功能在这里:
import requests
def sen_request():
url = "somewhere.com/somewhere.asmx/new_one"
payload = {}
headers= {}
response = requests.request("GET", url, headers=headers, data = payload)
return (response)
- 我尝试使用 response.json() 访问 JSON 中的响应,但出现此错误:
Exception Type: JSONDecodeError Exception Value: Expecting value: line 1 column 1 (char 0)
该代码使用json库和XML库解析以提取id
import json
import xml.etree.ElementTree as ET
data = '''<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"id":"1819","responseCode":"0","responseMessage":"0"}</string>'''
data1 = '''<?xml version="1.0" encoding="utf-8"?><string xmlns="tempuri.org">{"id":{"id1":"1819"},"responseCode":"0","responseMessage":"0"}</string>'''
root = ET.fromstring(data)
print(json.loads(root.text)['id'])
root = ET.fromstring(data1)
print(json.loads(root.text)['id']['id1'])
输出
1819
1819