微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在为硒选择一个随机URL时遇到错误,尽管数字在python列表中如何修复它

如何解决我在为硒选择一个随机URL时遇到错误,尽管数字在python列表中如何修复它

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。