在Python中使用for循环从列表中选择数值创建URL。

huangapple go评论54阅读模式
英文:

Creating URL's in for loop by picking values from list in Python

问题

为了创建GET请求,我创建了一个Python脚本。为了创建此请求的URL,我已经编写了以下代码:

today = str(datetime.date.today())
start = str(datetime.date.today() - datetime.timedelta(days=30))

report = ["Shifts",
          "ShiftStops",
          "ShiftStopDetailsByProcessDate",
          "TimeRegistrations",
          "ShiftsByProcessDate",
          "ShiftStopsByProcessDate",
          ]

for x in report:
    url_data = "https://URL" + report + "?from=" + start + "&until=" + today
    data = requests.get(url_data, headers={"Host": "services.URL.com", "Authorization": "Bearer " + access_token})

但是我得到的错误是:

TypeError: can only concatenate str (not "list") to str

我该如何解决这个问题并创建6个唯一的URL?

附:我已经在URL中添加了单词"URL"以匿名化我的帖子。

英文:

In order to create get-requests I create a Python script. In order to create the URL's for this request I have made the following code:

today = str(datetime.date.today())
start = str(datetime.date.today()- datetime.timedelta (days=30))

report = ["Shifts",
          "ShiftStops",
          "ShiftStopDetailsByProcessDate",
          "TimeRegistrations",
          "ShiftsByProcessDate",
          "ShiftStopsByProcessDate",
          ]

for x in report:
    url_data = "https://URL"+ report + "?from=" + start + "&until=" + today
    data = requests.get(url_data, headers = {'Host': 'services.URL.com', 'Authorization': 'Bearer ' + acces_token})

But the error I get is:

TypeError: can only concatenate str (not "list") to str

What can I do to solve this and create 6 unique url's?

p.s. I have added the word URL to the URL's in order to anonymize my post.

答案1

得分: 0

你出错的地方在以下这一行:

url_data = "https://URL" + report + "?from=" + start + "&until=" + today

具体来说,你使用了report,它是整个列表。你应该使用x,也就是列表中的字符串。

另外,你需要缩进下一行,所以完整的代码应该是:

for x in report:
    url_data = "https://URL" + x + "?from=" + start + "&until=" + today
    data = requests.get(url_data, headers = {'Host': 'services.URL.com', 'Authorization': 'Bearer ' + access_token})
英文:

Where you're going wrong is in the following line:

url_data = "https://URL"+ report + "?from=" + start + "&until=" + today

Specifically, you use report which is the entire list. What you'll want to do is use x instead, i.e. the string in the list.

Also you'll want to indent the next line, so altogether it should read:

for x in report:
    url_data = "https://URL"+ x + "?from=" + start + "&until=" + today
    data = requests.get(url_data, headers = {'Host': 'services.URL.com', 'Authorization': 'Bearer ' + acces_token})

答案2

得分: 0

我已经找到了答案。URL列表是通过在创建url_data时将report替换为x来创建的。

url_data = "https://URL" + x + "?from=" + start + "&until=" + today
英文:

I have already found the answer. The list of url's is created by replacing report by x while create the url_data.

url_data = "https://URL"+ x + "?from=" + start + "&until=" + today

huangapple
  • 本文由 发表于 2023年2月8日 22:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定