Input variable name as raw string into request in python

I am kind of very new to python. I tried to loop through an URL request via python and I want to change one variable each time it loops.

My code looks something like this:

codes = ["MCDNDF3","MCDNDF4"]

#count = 0
for x in codes:
    response = requests.get(url_part1 + str(codes) + url_part3, headers=headers)

I want to have the url change at every loop to like url_part1+code+url_part3 and then url_part1+NEXTcode+url_part3.

Sadly my request badly formats the string from the variable to “%5B'MCDNDF3'%5D“.

It should get inserted as a raw string each loop. I don’t know if I need url encoding as I don’t have any special chars in the request. Just change code to MCDNDF3 and in the next request to MCDNDF4.

Any thoughts?



In your for loop, the first line should be:

response = requests.get(url_part1 + x + url_part3, headers=headers)

This will work assuming url_part1 and url_part3 are regular strings. x is already a string, as your codes list (at least in your example) contains only strings. %5B and %5D are [ and ] URL-encoded, respectively. You got that error because you called str() on a single-membered list:

>>> str(["This is a string"])
"['This is a string']"

If url_part1 and url_part3 are raw strings, as you seem to indicate, please update your question to show how they are defined. Feel free to use if you don’t want to reveal your actual target URL. You should probably be calling str() on them before constructing the full URL.

Source: stackoverflow