嗨,我想循环链接,我从一个访问数据库检索,然后得到每个链接的次数我的代码如下问题是它得到第二个链接并停止
count=0
for link in df['Links']:
while count < 2:
driver = webdriver.Chrome(executable_path=path, options=options)
driver.get("" + link)
time.sleep(100)
driver.close()
count = count + 1
我认为在这种情况下使用for循环更有意义:
for link in df['Links']:
for _ in range(2):
driver = webdriver.Chrome(executable_path=path, options=options)
driver.get("" + link)
time.sleep(100)
driver.close()
_
是一个变量,很像x
或count
的作用,但通常在不使用变量时使用,例如本例。
将count=0
放入for循环中。否则,count保持为2,并且在for循环的第一次迭代之后跳过while循环中的所有内容。
您的代码明确地告诉您在第二次迭代时停止。
count=0 # sets up a counter
for link in df['Links']:
while count < 2: # Don't count more than this number
driver = webdriver.Chrome(executable_path=path, options=options)
driver.get("" + link)
time.sleep(100)
driver.close()
count = count + 1 # increase the counter for the next iteration
下面是将计数器放入for循环的修改:
for link in df['Links']:
count=0 # sets up a fresh counter each iteration
while count < 2: # Don't count more than this number
driver = webdriver.Chrome(executable_path=path, options=options)
driver.get("" + link)
time.sleep(100)
driver.close()
count = count + 1 # increase the counter for the next iteration