新程序 发表于 2025-6-25 11:28:20

批量自动浏览器访问url链接的程序

批量自动浏览器访问url链接的程序



创建一个python文件名为batch_url_visitor.py


代码程序


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os

def read_urls(file_path):
    """读取 URL 列表文件"""
    try:
      with open(file_path, 'r', encoding='utf-8') as file:
            # 去除空行和首尾空格
            urls =
      return urls
    except FileNotFoundError:
      print(f"错误:找不到文件 {file_path}")
      return []
    except Exception as e:
      print(f"读取文件时发生错误:{str(e)}")
      return []

def visit_urls(urls, wait_time=5):
    """使用 Chrome 浏览器访问 URL 列表"""
    # 设置 Chrome 选项
    chrome_options = Options()
    chrome_options.add_argument('--start-maximized')# 最大化窗口
   
    try:
      # 初始化浏览器
      driver = webdriver.Chrome(options=chrome_options)
      
      # 依次访问每个 URL
      for index, url in enumerate(urls, 1):
            print(f"正在访问第 {index}/{len(urls)} 个 URL: {url}")
            try:
                # 打开 URL
                driver.get(url)
                # 等待页面加载
                time.sleep(wait_time)
            except Exception as e:
                print(f"访问 {url} 时发生错误:{str(e)}")
                continue
      
      # 关闭浏览器
      driver.quit()
      print("所有 URL 访问完成!")
      
    except Exception as e:
      print(f"浏览器操作时发生错误:{str(e)}")
      if 'driver' in locals():
            driver.quit()

def main():
    # URL 列表文件路径
    url_file = "urls.txt"
   
    # 检查文件是否存在
    if not os.path.exists(url_file):
      print(f"请先创建 {url_file} 文件,并在每行写入一个 URL")
      return
   
    # 读取 URL 列表
    urls = read_urls(url_file)
   
    if not urls:
      print("没有读取到有效的 URL")
      return
   
    # 访问 URL
    visit_urls(urls, wait_time=5)

if __name__ == "__main__":
    main()



1.使用说明:

安装依赖:
pip install selenium

2.创建 urls.txt 文件:urls.txt

urls.txt里面批量放入url链接

3.运行程序:
python batch_url_visitor.py

功能特点:

自动读取 urls.txt 中的 URL 列表
使用 Chrome 浏览器自动访问每个 URL
每个页面默认等待 5 秒(可修改 wait_time 参数)
错误处理机制,遇到无效 URL 会继续处理下一个
最大化浏览器窗口
访问进度显示
注意事项:

确保网络连接稳定
某些网站可能有反爬机制,可能需要调整等待时间
大量 URL 访问可能需要添加随机延时以避免被封禁
使用前请确保遵守目标网站的访问政策




页: [1]
查看完整版本: 批量自动浏览器访问url链接的程序