You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python按顺序逐个打开列表中存储的所有网页链接

问题点梳理
  • 循环逻辑完全错误:迭代links时得到的i是字符串类型的链接,和整数k=24做大小比较会直接触发类型错误,且循环内没有任何打开链接的逻辑
  • 仅在循环外执行了1次打开首个链接的操作,没有实现遍历列表逐个打开的需求
  • 冗余导入了未使用的requests库
  • Selenium 4.0+版本已弃用executable_path参数,直接使用会抛出告警或报错
  • 没有添加浏览器驱动的释放逻辑,运行后会残留浏览器后台进程
修正后可运行代码
import time
from selenium import webdriver
from selenium.webdriver.firefox.service import Service

links = [
    "https://www.cbc.ca/arts/montreal-was-home-to-the-first-known-queer-magazine-in-north-america-from-1918-1920-1.6069620",
    "https://www.cbc.ca/archives/not-a-good-time-for-newspapers-the-1992-edition-1.5604471",
    "https://www.cbc.ca/archives/car-phones-helped-get-the-job-done-in-1990-1.5166262",
    "https://www.cbc.ca/news/canada/nova-scotia/africville-documentary-irvine-carvery-daisy-carvery-halifax-1.5936410",
    "https://www.cbc.ca/news/entertainment/frank-bonner-actor-herb-tarlek-dead-1.6070564",
    "https://www.cbc.ca/archives/drinks-of-the-70s-80s-90s-1.6060609",
    "https://www.cbc.ca/archives/when-goat-cheese-got-to-be-quite-a-fad-in-canada-1.5606611",
    "https://newsinteractives.cbc.ca/longform/overtime-in-vancouver",
    "https://www.cbc.ca/archives/it-wasn-t-skateboarding-in-1965-it-was-skurfing-1.5600339",
    "https://www.cbc.ca/archives/that-time-former-ndp-leader-ed-broadbent-made-a-comeback-with-a-rap-video-1.5160702",
    "https://www.cbc.ca/news/canada/sudbury/bowie-painting-northern-ontario-1.6064962",
    "https://www.cbc.ca/archives/the-meteorite-that-startled-cows-and-quebecers-in-1994-1.5598277",
    "https://www.cbc.ca/archives/when-2-prime-ministers-swapped-houses-1.5174481",
    "https://www.cbc.ca/news/entertainment/ned-beatty-obit-1.6064539",
    "https://www.cbc.ca/news/canada/north/styrofoam-igloos-kinngait-1.6060597",
    "https://www.cbc.ca/radio/spark/something-s-rotten-in-the-state-of-the-internet-and-archivists-are-worried-1.6047113",
    "https://www.cbc.ca/news/canada/nova-scotia/tram-tracks-uncovered-spring-garden-road-1.6063126",
    "https://www.cbc.ca/radio/unreserved/what-happens-when-hidden-histories-become-a-national-conversation-1.6059520/the-indigenous-connection-to-the-underground-railroad-1.6061957",
    "https://www.cbc.ca/archives/cancon-junos-radio-music-industry-1.6050466",
    "https://www.cbc.ca/archives/taking-the-census-wasn-t-easy-in-1961-1.5158058",
    "https://www.cbc.ca/news/canada/montreal/first-person-indigenous-representation-museums-culture-history-1.6059501",
    "https://www.cbc.ca/news/canada/ottawa/mcarthur-bowling-alley-closed-1.6060365",
    "https://www.cbc.ca/arts/how-the-rosie-o-donnell-show-changed-celebrity-culture-forever-1.6059882",
    "https://www.cbc.ca/archives/when-canadian-wine-cost-too-much-for-canadian-consumers-1.5596783",
]

# 若geckodriver已配置到系统环境变量,可直接省略executable_path参数
s = Service(executable_path=r'geckodriver.exe')
driver = webdriver.Firefox(service=s)

try:
    # 遍历所有链接逐个打开
    for url in links:
        driver.get(url)
        time.sleep(5)
finally:
    # 无论运行是否报错都关闭浏览器和驱动,避免残留进程
    driver.quit()
补充说明
  • 上述代码会在同一个Firefox窗口中依次打开所有链接,每个页面停留5秒后自动跳转下一个
  • 如果需要每个链接在新标签页打开,可在循环内添加打开新标签的逻辑
  • 若运行提示找不到geckodriver,请确认geckodriver文件路径正确,或已将其所在目录添加到系统环境变量

内容的提问来源于stack exchange,提问作者ludon5681

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 10:15:02