当前位置: 首页> 汽车> 时评 > a5站长网_小程序制作教程零基础入门_知乎关键词搜索_目前在哪个平台做推广好

a5站长网_小程序制作教程零基础入门_知乎关键词搜索_目前在哪个平台做推广好

时间:2025/7/10 5:32:37来源:https://blog.csdn.net/u010799534/article/details/142557244 浏览次数: 0次
a5站长网_小程序制作教程零基础入门_知乎关键词搜索_目前在哪个平台做推广好

python自动化过程中,经常遇到脚本跑着跑着,就报错,一看,浏览器自动升级了。
共两张解决方案:
1、禁止浏览器自动升级
2、当脚本运行前先去检测当前的chromedriver与浏览器是否匹配,不匹配,就下载,然后将下载的chromedriver放置到指定位置。
本篇主要是记录方案2的实现,主要依靠webdriver_manager库来实现,代码如下:

import os
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import shutil"""使用须知:1.安装webdriver_manager库pip install webdriver_manager -i https://pypi.tuna.tsinghua.edu.cn/simple
"""def update_chrome_driver(path) -> webdriver:""":param path: 下载后的chromedriver存放位置:return:"""try:wb = webdriver.Chrome()print("当前已是最新的chromedriver")wb.quit()except Exception as e:print("更新驱动中……")driver_path = ChromeDriverManager().install()start = driver_path.rindex('/')src_path = driver_path[:start + 1]print(f"下载的路径:{src_path}")copy_dirs(src_path, path)print("===========更新驱动完成============")def copy_dirs(src_path, target_path):''':param src_path::param target_path::return:'''file_count = 0source_path = os.path.abspath(src_path)target_path = os.path.abspath(target_path)if not os.path.exists(target_path):os.makedirs(target_path)if os.path.exists(source_path):for root, dirs, files in os.walk(source_path):for file in files:src_file = os.path.join(root, file)shutil.copy(src_file, target_path)file_count += 1print(f"=====移动的文件========>{src_file}")return int(file_count)if __name__ == '__main__':use_path = "你想要存放chromedriver的路径"update_chrome_driver(use_path)

以后每次执行脚本前,执行改文件即可,已经在mac和windows环境下验证过。
还有一点需要注意,use_path的路径是否有写入的权限!
mac电脑可以看一下这篇文章: macOs修改文件目录

关键字:a5站长网_小程序制作教程零基础入门_知乎关键词搜索_目前在哪个平台做推广好

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: