admin管理员组

文章数量:1665521

文章目录

    • 1.为什么需要便携式谷歌浏览器?
    • 2.如何制作便携式谷歌浏览器
      • 2.1 提取启动器GoogleChromePortable.exe
      • 2.2 下载官方离线包
      • 2.3 制作离线包
      • 2.4 禁用谷歌浏览器升级提示
    • 3.使用python来自动化生成

1.为什么需要便携式谷歌浏览器?

我们尝试问下ai

但对于浏览器自动化而言,好处还是很明显的:

  1. 固定浏览器版本,不用担心浏览器自动生成导致驱动不匹配
  2. 某些场景只能固定某些版本使用,例如Chrome 72禁用对TLS 1.0和TLS 1.1的支持,而ccb网银就需要老版本的tls,换成72版本之前即可
  3. 程序分发时可以直接打包chrome进去,而无需担心用户电脑环境是否安装了目标版本的浏览器

2.如何制作便携式谷歌浏览器

2.1 提取启动器GoogleChromePortable.exe

访问google_chrome_portable下载

下载得到GoogleChromePortable_125.0.6422.142_online.paf.exe,版本号可能不一致

解压得到GoogleChromePortable.exe

2.2 下载官方离线包

  • https://chrome.wums/
  • https://www.chromedownloads/chrome64win-stable/

可按需下载

2.3 制作离线包

步骤:

  • 将chrome离线包107.0.5304.122_chrome64_stable_windows_installer.exe更改名称为107.0.5304.122_chrome64_stable_windows_installer.zip(版本号根据实际情况来)
  • 解压该zip
  • 在解压完的文件中找到chrome.7z,解压chrome.7z
  • 创建App目录,将解压完的Chrome-bin目录放到其中
  • 创建data目录,存放浏览器配置信息
  • 将GoogleChromePortable.exe放到同级目录,改名chrome.exe
  • 便携式谷歌浏览器制作完毕,通过chrome.exe启动即可

最终结构如下:

2.4 禁用谷歌浏览器升级提示

启动时添加--disable-background-networking 启动参数即可

3.使用python来自动化生成

备注:使用了Bandizip.exe来自动化解压,需要安装Bandizip并将Bandizip.exe添加到环境变量

import logging as log  
import shutil  
import subprocess  
from pathlib import Path  
  
log.basicConfig(level=log.INFO, format='%(asctime)s - %(levelname)s - %(message)s')  
  
  
class LocalChrome():  
    def __init__(self,chrome_path):  
        # 备份chrome,将exe改名为zip  
        self.chrome_path = chrome_path  
        self.chrome_dir = Path(self.chrome_path).parent  
        self.chrome_zip = Path(self.chrome_dir).joinpath(Path(self.chrome_path).name.replace(".exe",".zip"))  
    
    def backup(self):  
        # 备份chrome,将exe改名为zip  
        if Path(self.chrome_path).exists():  
            if self.chrome_zip.exists():  
                return  
            shutil.copy(self.chrome_path,self.chrome_zip)  
        else:  
            log.error("chrome not exists")  
  
    def unzip(self):  
        # 解压zip  
        if self.chrome_zip.exists():  
            # 编写解压代码  
            unzip_dir = self.chrome_zip.parent.joinpath(self.chrome_zip.name.replace(".zip",""))  
            if unzip_dir.exists():  
                shutil.rmtree(unzip_dir)  
            unzip_dir.mkdir()  
            self.unzip_file(self.chrome_zip,unzip_dir)  
        else:  
            log.error("chrome zip not exists")  
  
    @staticmethod  
    def unzip_file(zip_file_path, extract_dir):  
        extract_dir = Path(extract_dir)  
        if extract_dir.exists():  
            shutil.rmtree(extract_dir)  
        extract_dir_str = str(extract_dir.resolve())  
        if not extract_dir_str.endswith("\\"):  
            extract_dir_str += "\\"  
        res = subprocess.check_output(['Bandizip.exe','x','-y','-o:%s' % extract_dir_str, zip_file_path])  
        log.info(res)  
  
  
if __name__=="__main__":  
    # GoogleChromePortable路径
    chrome_portable = r"D:\chrome\GoogleChromePortable.exe"  
    # 离线包路径
    chrome_path = r"D:\chrome\107.0.5304.122_chrome64_stable_windows_installer.exe"  
    zip_file_path = chrome_path.replace(".exe",".zip")  
    extract_dir = zip_file_path.replace(".zip","")  
    chrome = LocalChrome(chrome_path =chrome_path)  
    chrome.backup()  
    # 解压zip文件  
    chrome.unzip_file(zip_file_path,extract_dir)  
    # 解压7z文件  
    chrome7z = Path(extract_dir).joinpath("chrome.7z")  
    chrome7z_path = str(chrome7z.resolve())  
    chrome7z_exact_path = chrome7z_path.replace(".7z", "")  
    chrome7z_exact_path_str = str(Path(chrome7z_exact_path).resolve())  
    chrome.unzip_file(chrome7z_path,chrome7z_exact_path_str)  
    # 创建目标目录  
    zip_file = Path(zip_file_path)  
    zip_file_name = zip_file.name  
    # 获取版本号  
    version = zip_file_name.split("_chrome")[0]  
    target_dir = zip_file.parent.joinpath("chrome_%s" % version)  
    if target_dir.exists():  
        shutil.rmtree(target_dir)  
    # 创建App目录 data目录  
    app_dir = target_dir.joinpath("App")  
    app_dir.mkdir(parents=True,exist_ok=True)  
    data_dir = target_dir.joinpath("data")  
    data_dir.mkdir(parents=True,exist_ok=True)  
    # 复制文件  
    shutil.copytree(chrome7z_exact_path_str+"\\Chrome-bin",str(app_dir.joinpath("Chrome-bin").resolve()))  
    shutil.copy(chrome_portable,str(target_dir.joinpath("chrome.exe").resolve()))  
    # 删除文件  
    Path(zip_file_path).unlink()  
    shutil.rmtree(extract_dir)

本文标签: 浏览器chrome