1
0

update_chrome.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import json
  2. import os
  3. import re
  4. import sys
  5. import requests
  6. import platform
  7. import shutil
  8. import zipfile
  9. import urllib.request
  10. def download_and_extract_zip(url, destination_folder):
  11. # 下载ZIP文件
  12. urllib.request.urlretrieve(url, "temp.zip")
  13. # 解压ZIP文件
  14. with zipfile.ZipFile("temp.zip", "r") as zip_ref:
  15. zip_ref.extractall(destination_folder)
  16. # 删除临时ZIP文件
  17. os.remove("temp.zip")
  18. def copy_file(source_file, destination_file):
  19. # 使用copy2()函数复制文件
  20. shutil.copy2(source_file, destination_file)
  21. def copy_folder(source_folder, destination_folder):
  22. # 使用copytree()函数复制文件夹及其内容
  23. shutil.copytree(source_folder, destination_folder)
  24. update_version = "115" # 要更新的chromedriver版本
  25. chrome_driver_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
  26. win64_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  27. win32_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  28. mac_chrome_path = "/Applications/Google Chrome.app/Contents/MacOS"
  29. linux_chrome_path = "/opt/google/chrome"
  30. if __name__ == "__main__":
  31. driver_downloads = []
  32. response = requests.get(chrome_driver_url)
  33. if response.status_code == 200:
  34. versions = json.loads(response.content)["versions"]
  35. versions = versions[::-1] # 倒序排列数组
  36. for info in versions:
  37. version = info["version"]
  38. if version.find(update_version) >= 0:
  39. downloads = info["downloads"]
  40. if "chromedriver" in downloads:
  41. print(info["version"])
  42. driver_downloads = downloads["chromedriver"]
  43. break
  44. else:
  45. print("Error: " + response.status_code)
  46. exit(1)
  47. if os.path.exists("./chromedrivers"):
  48. shutil.rmtree("./chromedrivers")
  49. os.mkdir("./chromedrivers")
  50. if sys.platform == "win32" and platform.architecture()[0] == "64bit":
  51. for download in driver_downloads:
  52. if download["platform"] == "win64":
  53. url = download["url"]
  54. print(url)
  55. break
  56. download_and_extract_zip(url, "./chromedrivers")
  57. if os.path.exists("./chrome_win64"):
  58. shutil.rmtree("./chrome_win64")
  59. copy_folder(win64_chrome_path, "./chrome_win64")
  60. for folder in os.listdir("./chrome_win64"):
  61. if folder[0].isdigit() and os.path.isdir("./chrome_win64/"+folder):
  62. shutil.rmtree("./chrome_win64/"+folder+"/Installer") # 删除Installer文件夹
  63. copy_file("./execute.bat", "./chrome_win64/execute.bat")
  64. copy_file("./stealth.min.js", "./chrome_win64/stealth.min.js")
  65. copy_file("./chromedrivers/chromedriver-win64/chromedriver.exe", "./chrome_win64/chromedriver_win64.exe")
  66. elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
  67. for download in driver_downloads:
  68. if download["platform"] == "win32":
  69. url = download["url"]
  70. print(url)
  71. break
  72. download_and_extract_zip(url, "./chromedrivers")
  73. if os.path.exists("./chrome_win32"):
  74. shutil.rmtree("./chrome_win32")
  75. copy_folder(win64_chrome_path, "./chrome_win32")
  76. for folder in os.listdir("./chrome_win32"):
  77. if folder[0].isdigit() and os.path.isdir("./chrome_win32/"+folder):
  78. shutil.rmtree("./chrome_win32/"+folder+"/Installer") # 删除Installer文件夹
  79. copy_file("./execute.bat", "./chrome_win32/execute.bat")
  80. copy_file("./stealth.min.js", "./chrome_win32/stealth.min.js")
  81. copy_file("./chromedrivers/chromedriver-win32/chromedriver.exe", "./chrome_win32/chromedriver_win32.exe")
  82. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  83. pass
  84. elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  85. pass
  86. print("Done and don't forget to generate executestage EXEcutable program!")