如何设置特定范围,以使其采用该范围的值并在python中运行

时间:2019-07-16 12:59:15

标签: python loops selenium for-loop selenium-webdriver

具有包含链接的python代码

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

brand=55处应该有forloop范围,以便它取(1-500)中的值并运行代码。

代码:

#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 : 
....

如何添加for循环使其在(1-500)范围内循环。

喜欢

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')

1 个答案:

答案 0 :(得分:1)

如果您使用的是Python 3.5以上版本,则可以使用f字符串:

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

否则,您可以使用%格式语法:

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

str.format语法

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