问题描述
from selenium import webdriver
import time
import random
driver = webdriver.Chrome()
videos = [
'https://www.youtube.com/watch?v=HPLJ-k8Mt-I'
'https://www.youtube.com/watch?v=nN1XDURBNYo'
'https://www.youtube.com/watch?v=KUTXpFO2yDo'
'https://www.youtube.com/watch?v=McclaedI05g'
'https://www.youtube.com/watch?v=eHMSMGfkGjY'
'https://www.youtube.com/watch?v=c86GdHQaLsY'
'https://www.youtube.com/watch?v=dz59GsdvUF8'
'https://www.youtube.com/watch?v=Xjv1sY630Uc'
]
for i in range(1000):
print("vid is running for",i,)
randomvideo = random.randint(0,3)
print(randomvideo)
driver.get(videos[randomvideo])
sleep_time = random.randint(5,10)
time.sleep(sleep_time)
driver.quit()
输出是
Traceback (most recent call last):
File "/home/asus/PycharmProjects/pythonProject2/selenum practice.py",line 21,in <module>
driver.get(videos[randomvideo])
IndexError: list index out of range
如何解决此问题!!!! 该值来自列表之一 抱歉!这是第一次使用stackoverflow 请帮忙
解决方法
应该这样写
videos = [
'https://www.youtube.com/watch?v=HPLJ-k8Mt-I','https://www.youtube.com/watch?v=nN1XDURBNYo','https://www.youtube.com/watch?v=KUTXpFO2yDo','https://www.youtube.com/watch?v=MccLaedI05g','https://www.youtube.com/watch?v=eHMSMGfkGjY','https://www.youtube.com/watch?v=c86GdHQaLsY','https://www.youtube.com/watch?v=dz59GsdvUF8','https://www.youtube.com/watch?v=Xjv1sY630Uc'
]