具有包含链接的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')
答案 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