Insert different variable (pulled from dictionary) into URL within a loop

72 Views Asked by At

I would like to create a loop that visits a different URL based on a variable that is inserted into the URL. There are dozens of variables to be inserted, and I'm thinking there's an alternative approach to what I have below:

url1 = "cat"
url2 = "dog"
url3 = "mouse"

driver.get("https://www.google.com/webhp?hl=en#hl=en&q=" +url1)
# do stuff

driver.get("https://www.google.com/webhp?hl=en#hl=en&q=" +url2)
# do stuff

driver.get("https://www.google.com/webhp?hl=en#hl=en&q=" +url3)
# do stuff

Is is possible to create a loop that grabs every url variable without my having to create a line for each one?

1

There are 1 best solutions below

0
On BEST ANSWER
url_list = ["cat", "dog", "mouse"]

for url in url_list:
    driver.get("https://www.google.com/webhp?hl=en#hl=en&q=" + url)
    # do stuff