You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历SOAP协议的DMSId与GroupActionId参数并拼接结果?

问题解决:循环迭代SOAP请求参数并收集所有结果

你的代码存在两个核心问题:

  • 请求语句requests.post位于内层循环之外,导致仅会使用最后一组iddms和idaction参数发起请求,之前的迭代完全无效
  • 没有对每次请求的结果进行收集,最终仅返回单次请求的内容

修正后的代码如下:

import requests

def get_xml():
    # 固定配置移至循环外,避免重复定义
    url = "http://url.com/soap/IWSDialog"
    headers = {"content-type": "application/soap+xml; charset=utf-8"}
    # 初始化列表存储所有请求结果
    all_action_lists = []
    
    for iddms in ['7','16','15','10','25','8','11','12','14','13']:
        for idaction in ['1','2']:
            body = """<soapenv:Envelope>
               <soapenv:Header/>
                   <soapenv:Body>
                          <urn:getActionList>
                             <EntId>1013</EntId>
                             <DMSId>{}</DMSId>
                             <GroupActionId>{}</GroupActionId>
                          </urn:getActionList>
                   </soapenv:Body>
                </soapenv:Envelope>""".format(iddms, idaction)
            # 在内层循环内发起请求,确保每组参数都被调用
            response = requests.post(url, data=body, headers=headers)
            # 将当前请求结果追加到集合中
            all_action_lists.append(response.content)
    
    # 返回所有收集到的结果
    return all_action_lists

ActionList = get_xml()

关键改进点:

  • 使用format()方法拼接SOAP请求体,比字符串拼接更易读和维护
  • 将固定的url和headers移至循环外部,减少不必要的重复创建
  • 新增all_action_lists列表,用于存储每次请求的响应内容
  • 将请求逻辑移入内层循环,确保每一组DMSId和GroupActionId组合都会发起请求
  • 最终返回所有结果的集合,而非单次请求结果

如果需要对返回的SOAP响应进行解析(比如提取具体的预约数据),可以在追加结果前加入XML解析逻辑(比如使用xml.etree.ElementTree或lxml库)。

内容的提问来源于stack exchange,提问作者wysouf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:36:29