赞
踩
代码:
def buildNewsXmlFile(): # 设置一个新节点,并设置其标签为root root = ET.Element("root") # 在root下新建两个子节点,设置其名称分别为sina和chinabyte sina = ET.SubElement(root, "sina") chinabyte = ET.SubElement(root, "chinabyte") # 在sina下新建两个子节点,设置其节点名称分别为number和first sina_number = ET.SubElement(sina, "number") sina_number.text = "1" sina_first = ET.SubElement(sina, "first") sina_first.text = "http://roll.tech.sina.com.cn/internet_all/index_1.shtml" # 在chinabyte下新建两个子节点,设置其节点名称为number和first chinabyte_number = ET.SubElement(chinabyte, "number") chinabyte_number.text = "1" chinabyte_first = ET.SubElement(chinabyte, "first") chinabyte_first.text = "http://www.chinabyte.com/more/124566.shtml" # 将节点数信息保存在ElementTree中,并且保存为XML格式文件 tree = ET.ElementTree(root) # tree = ET.ElementTree(root,encoding='utf8') 后面加编码的话,会在生成的etree树最外层加上</xml ...的格式. tree.write("1111111111111.xml")
输出:
<root>
<sina>
<number>1</number>
<first>http://roll.tech.sina.com.cn/internet_all/index_1.shtml</first>
</sina>
<chinabyte>
<number>1</number>
<first>http://www.chinabyte.com/more/124566.shtml</first>
</chinabyte>
</root>
使用requests请求:
ats_xml_text = r'''<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdd="http://xml.fa-software.com/mycim/wsdd/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <soapenv:Header/> %s </soapenv:Envelope>''' def get_aoiDefectItem(date:dict): aoiDefect = ET.Element("aoiDefect") for key,value in date.items(): el = ET.SubElement(aoiDefect, key) if isinstance(value,list): for i in value: aoiDefectItem = ET.SubElement(el, "aoiDefectItem") defectLocaton = ET.SubElement(aoiDefectItem, "defectLocaton") defectType = ET.SubElement(aoiDefectItem, "defectType") for key, value in i.items(): defectLocaton.text = key defectType.text = value else: el.text = str(value) xmlstr=ET.tostring(aoiDefect) # print(xmlstr) a = ats_xml_text % xmlstr.decode() # 先解码拼接字符串 return a data_2 ={"aiResult": "OK", "result": "NG", "createTime": "2023-05-05 09:45:14", "eqptId": "666666666", "guiRoute": "D:/test/el/pic/LRR9057_8.png", "route": "D:/test/el/pic/LRR9057_8.png", "defects": {"隐": ["3-11", "4-10", "4-8", "5-11"], "痕": ["4-17", "4-13", "6-14"]}, "aoiDefectItems": [{"隐": "3-11,4-10,4-8,5-11"}, {"痕": "4-17,4-13,6-14"}], "lotId": "LRR9057_8", "side": "B" } data_ddd,b = get_aoiDefectItem(data_2) data = data_ddd.encode('utf8') # 编码放入data中,requests的data不能传递字符串. response = requests.request('post', url=url, data=data, headers=headers, timeout=5).text
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。