pythonpython-3.xstringfor-loopgoogle-search-api

replace a substring inside a for loop


I am running an API query like this. Instead of "montratec" here q=montratec, I want to use various different words that I have stored in a list. I want to run this inside a for loop by replacing the word montratec with different elements in the list names. How can I achieve this?

url = "https://google-search3.p.rapidapi.com/api/v1/crawl/q=montratec%20AND%20(katalog%20OR%20catalog%20OR%20brosch%C3%BCre%20OR%20brochure)%20AND%20filetype=pdf&num=5"
response = requests.request("GET", url, headers=headers)
for i in names:
    url = "https://google-search3.p.rapidapi.com/api/v1/crawl/q=montratec%20AND%20(katalog%20OR%20catalog%20OR%20brosch%C3%BCre%20OR%20brochure)%20AND%20filetype=pdf&num=5" 

Solution

  • try the below (use "f" string)

    for name in names:
        url = f"https://google-search3.p.rapidapi.com/api/v1/crawl/q={name}%20AND%20(katalog%20OR%20catalog%20OR%20brosch%C3%BCre%20OR%20brochure)%20AND%20filetype=pdf&num=5"