首页 > 编程知识 正文

pycharm和python区别,chrome兼容性站点

时间:2023-05-06 00:52:17 阅读:134242 作者:4548

如果使用python selenium驱动chrome浏览器,则需要chromedriver的支持,但chrome浏览器会频繁更新。 chrome浏览器和chrome驱动程序必须保持版本一致性。 在这种情况下,需要手动下载chromedriver以匹配此时的浏览器,这时我想是不是有代替我们完成这项工作的程序。

想法

将当前chrome浏览器的版本号与chromedriver的浏览号进行比较

如果不匹配,请下载新的chromedriver并将其替换为原始驱动程序

否则我什么都不做

chrome的版本通常通过在浏览器地址栏中键入chrome://settings/help来查看:

读取chrome浏览器的版本信息,读取文件产品的版本信息。 (必须预先安装pywin32模块),返回值为版本号字符串) 89.0.4389.90 ) :

from win32 com.clientimportdispatchallinfo=dispatch (' scripting.file system object ' ) version=allinfo.getfileversion

importwinregdefget _ chrome _ version (: key=winreg.openkey (winreg.HKEY _ current _ ser,r ' software _ ger ttte ) CD位于chromedriver所在的目录中,使用命令。 3 chromedriver.exe --V获取版本号并返回结果。 chrome driver 89.0.4389.23 (61b 08 ee 2c 50024 Bab 004 e 48 D2 b1b 083 CDB DAC 579-refs/branch-h ranch python )如何与终端交互其实很简单:

import osdef get_version () :“”查询系统中的Chromedriver版本“' out STD=OS.popen ' ' chrome driver-version '”

“http://NPM.Tao Bao.org/mirrors/chrome driver/89.0.4389.23 /

importrequestsdefdownload _ driver (download _ URL ) : ' ' '下载文件' ' file=requests.get ) download_URL ' . write(file.content ) print ) (下载成功) )也可以指定下载的chromedriver.zip文件的完整路径

3 .定义自动解压缩zip文件的附加方法。 自动解压缩zip文件,获取中的chromedriver.exe文件,并自动复盖旧版本的chrome driver。

importzipfiledefunzip _ driver (path ) :“”是用于指定Chromedriver压缩包的目录“”(f=zipfile.zipfile (chrome driver.ziver.ze ) ) t )文件, path )4.以下是一个完整的程序,可以直接编写工具调用#-*-coding 3360 utf-8-*-importreeserver的OS URL=' http://NPM.Taobao.orao . chromedriverdownloadlinkdefget _ chrome _ version (3360 key=winreg.openkey (winreg.HKEY _ current _ user,r ' softid ) )

(url): '''查询最新的Chromedriver版本''' rep = requests.get(url).text time_list = [] # 用来存放版本时间 time_version_dict = {} # 用来存放版本与时间对应关系 result = re.compile(r'd.*?/</a>.*?Z').findall(rep) # 匹配文件夹(版本号)和时间 for i in result: time = i[-24:-1] # 提取时间 version = re.compile(r'.*?/').findall(i)[0] # 提取版本号 time_version_dict[time] = version # 构建时间和版本号的对应关系,形成字典 time_list.append(time) # 形成时间列表 latest_version = time_version_dict[max(time_list)][:-1] # 用最大(新)时间去字典中获取最新的版本号 return latest_versiondef get_server_chrome_versions(): '''return all versions list''' versionList=[] url="http://npm.taobao.org/mirrors/chromedriver/" rep = requests.get(url).text result = re.compile(r'd.*?/</a>.*?Z').findall(rep) for i in result: # 提取时间 version = re.compile(r'.*?/').findall(i)[0] # 提取版本号 versionList.append(version[:-1]) # 将所有版本存入列表 return versionListdef download_driver(download_url): '''下载文件''' file = requests.get(download_url) with open("chromedriver.zip", 'wb') as zip_file: # 保存文件到脚本所在目录 zip_file.write(file.content) print('下载成功')def get_version(): '''查询系统内的Chromedriver版本''' outstd = os.popen('chromedriver --version').read() return outstd.split(' ')[1]def unzip_driver(path): '''解压Chromedriver压缩包到指定目录''' f = zipfile.ZipFile("chromedriver.zip",'r') for file in f.namelist(): f.extract(file, path)def get_path(): outstd1 = os.popen('where chromedriver').read() fpath, fname = os.path.split(outstd1) return fpathdef check_update_chromedriver(): chromeVersion=get_Chrome_version() chrome_main_version=int(chromeVersion.split(".")[0]) # chrome主版本号 driverVersion=get_version() driver_main_version=int(driverVersion.split(".")[0]) # chromedriver主版本号 download_url="" if driver_main_version!=chrome_main_version: print("chromedriver版本与chrome浏览器不兼容,更新中>>>") versionList=get_server_chrome_versions() if chromeVersion in versionList: download_url=f"{url}{chromeVersion}/chromedriver_win32.zip" else: for version in versionList: if version.startswith(str(chrome_main_version)): download_url=f"{url}{version}/chromedriver_win32.zip" break if download_url=="": print("暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。") download_driver(download_url=download_url) path = get_path() unzip_driver(path) os.remove("chromedriver.zip") print('更新后的Chromedriver版本为:', get_version()) else: print("chromedriver版本与chrome浏览器相兼容,无需更新chromedriver版本!")if __name__=="__main__": check_update_chromedriver()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。