user11669928
user11669928

Reputation:

How to set a particular range so that it takes the value of that range and run in python

have this python code that has a link present

driver.get('https://simpletire.com/catalog?select=1&brand=1')

in the place of brand=55 there should be forloop range so that it takes values from (1-500) and run the code .

code :

#Importing packages
from selenium import webdriver
import pandas as pd
import time

driver = webdriver.Chrome('/Users/1/chromedriver.exe')


driver.get('https://simpletire.com/catalog?select=1&brand=1')

try:

    click_more = True

    while click_more:


        time.sleep(5)
        element = driver.find_element_by_css_selector(".btn.btn-primary.btn-lg").click()


except : 
....

how to add for loop so that it loops in range from (1-500).

like

driver.get('https://simpletire.com/catalog?select=1&brand=1')
driver.get('https://simpletire.com/catalog?select=1&brand=2')
driver.get('https://simpletire.com/catalog?select=1&brand=3')
driver.get('https://simpletire.com/catalog?select=1&brand=4')
driver.get('https://simpletire.com/catalog?select=1&brand=5')
driver.get('https://simpletire.com/catalog?select=1&brand=6')

...... ...... ..... ..... driver.get('https://simpletire.com/catalog?select=1&brand=500')

Upvotes: 0

Views: 47

Answers (1)

C.Nivs
C.Nivs

Reputation: 13106

If you are in python version 3.5+, you can use an f-string:

for i in range(1, 501):
    site = f'https://simpletire.com/catalog?select1&brand={i}'
    driver.get(site)
    # rest of code

Otherwise, you can use % formatting syntax:

for i in range(1, 501):
    site = 'https://simpletire.com/catalog?select1&brand=%d' % i
    driver.get(site)
    # rest of code

Or the str.format syntax

for i in range(1, 501):
    site = 'https://simpletire.com/catalog?select1&brand={}'.format(i)
    driver.get(site)
    # rest of code

Upvotes: 1

Related Questions