Python-对具有动态值的列表中的每个项目发送请求 [英] Python - Send a request for each item in a list with dynamic values

查看:97
本文介绍了Python-对具有动态值的列表中的每个项目发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与UPS Package Tracking API一起使用,以通过POST请求发送跟踪号,该API接收该请求并发送回有关该软件包信息的JSON响应.

I'm currently working with the UPS Package Tracking API to send a tracking number via a POST request that the API receives and sends back a JSON response with information about the package.

当前,我只是使用几个嵌套的字典来处理单个跟踪号值的实际请求,但似乎无法弄清楚如何使它每次发送具有不同跟踪号的请求.

Currently, I just got the actual request working with a single tracking number value using several nested dictionaries, but I can't seem to figure out how to make it send the request with a different tracking number each time.

以下代码:

trk = ["tracknbr1", "tracknbr2", "tracknbr3"]

data = {"UPSSecurity" : {
        "UsernameToken" : {
            "Username" : nme,
            "Password" : pwd
        },
        "ServiceAccessToken" : {
            "AccessLicenseNumber" : aln
        },
    },
    "TrackRequest" : {
        "Request" : {
            "RequestOption" : 1,
            "TransactionReference" : {
                "CustomerContext" : ""
            },
        },
        "InquiryNumber" : trk
    }
    }

req = requests.post("https://wwwcie.ups.com/rest/Track", json=data)
print (req.text)

"查询编号"是跟踪编号存储在请求中的位置.

"InquiryNumber" is where the tracking number is stored inside the request.

(很抱歉,如果显示的大部分代码对于该问题都是不必要的,以为这会使我的问题更容易理解.)

因此,现在它仅发送对列表中第一项的请求,然后打印出响应.

So, right now it is only sending the request for the first item in the list, and then printing out the response.

我正在尝试找到一种方法,使请求遍历列表中的每个项目,每次发送带有下一个跟踪号的请求.

I am trying to figure out a way to have the request loop through each item in the list, sending the request with the next tracking number each time.

我用for循环尝试了几种不同的方法,但是我无法让它遍历每个请求的列表内的实际值.

I've tried several different methods with for loops, but I cannot get it to iterate through the actual values inside the list for each request.

可能没有一个明显的解决方案,因为我或多或少只是从Python开始的.让我知道你们的想法.

There may be an obvious solution I am not seeing, as I am more or less just starting with Python. Let me know what you guys think.

推荐答案

在示例中,您正在将trk变量用于不同的用途(对于list,然后对于单个InquiryNumber).

You are reusing trk variable for different things (for a list and then for a single InquiryNumber) in your example.

import requests


nme = 'your_username'
pwd = 'your_password'
aln = 'your_accesslicensenumber'

tracking_numbers = ["tracknbr1", "tracknbr2", "tracknbr3"]

for trk in tracking_numbers:
    data = {"UPSSecurity" : {
            "UsernameToken" : {
                "Username" : nme,
                "Password" : pwd
            },
            "ServiceAccessToken" : {
                "AccessLicenseNumber" : aln
            },
        },
        "TrackRequest" : {
            "Request" : {
                "RequestOption" : 1,
                "TransactionReference" : {
                    "CustomerContext" : ""
                },
            },
            "InquiryNumber" : trk
        }
        }

    response = requests.post("https://wwwcie.ups.com/rest/Track", json=data)
    print(response.json())

这篇关于Python-对具有动态值的列表中的每个项目发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆