1,将xml转换为字符串:
from xml.dom.minidom import parse doc = parse(xml_path) xml_str = doc.toprettyxml() return xml_str
2,获得前边获得字符串类型的xml
def request_post_json(urls, j_data, i): req= urllib.request.Request(url=urls,headers=headers,data=bytes(j_data, encoding='utf-8')) page = urllib.request.urlopen(req) res = page.read() page.close() json_result = json.loads(res.decode('utf8')) print('result==============',xmltodict.parse(json_result['result_xml'],attr_prefix="",cdata_key='')['ecg_fragment']['avg_hr'])
3,上边通过urllib进行访问并返回结果
调用xmltodict,将xml转换为dict,进行读取属性
print('result==============',xmltodict.parse(json_result['result_xml'],attr_prefix="",cdata_key='')['ecg_fragment']['avg_hr'])