Creating URL's in for loop by picking values from list in Python
Question:
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.
Answers:
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})
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
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.
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})
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