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) print(response.content) print(response.status_code) print(response.url)
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 “
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.
for loop, the first line should be:
response = requests.get(url_part1 + x + url_part3, headers=headers)
This will work assuming
url_part3 are regular strings.
x is already a string, as your
codes list (at least in your example) contains only strings.
] 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']"
url_part3 are raw strings, as you seem to indicate, please update your question to show how they are defined. Feel free to use
example.com if you don’t want to reveal your actual target URL. You should probably be calling
str() on them before constructing the full URL.