【问题标题】:Creating URL's in for loop by picking values from list in Python通过从 Python 列表中选取值在 for 循环中创建 URL
【发布时间】:2023-02-08 23:04:27
【问题描述】:

为了创建获取请求,我创建了一个 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 ' + 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. 

【问题讨论】:

  • "https://URL"+ report 应该是"https://URL"+ x 并修复缩进

标签: python python-3.x visual-studio-code


【解决方案1】:

您出错的地方在以下行中:

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 ' + acces_token})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2023-03-27
    • 2022-01-19
    • 2018-07-25
    • 1970-01-01
    相关资源
    最近更新 更多