Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

228 rindas
8.4 KiB

pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
  1. import json
  2. import os
  3. import re
  4. import subprocess
  5. import sys
  6. import requests
  7. import platform
  8. import shutil
  9. import zipfile
  10. import urllib.request
  11. if sys.platform == "win32":
  12. import winreg
  13. import re
  14. def get_processor_info():
  15. if os.uname().sysname == 'Darwin':
  16. processor_info = subprocess.check_output(['sysctl', '-n', 'machdep.cpu.brand_string']).strip()
  17. processor_info = str(processor_info)
  18. if 'Intel' in processor_info:
  19. return 'Intel'
  20. elif 'Apple' in processor_info:
  21. return 'Apple'
  22. else:
  23. return 'Unknown'
  24. else:
  25. return 'This method is only implemented for macOS.'
  26. def download_and_extract_zip(url, destination_folder):
  27. # 下载ZIP文件
  28. urllib.request.urlretrieve(url, "temp.zip")
  29. # 解压ZIP文件
  30. with zipfile.ZipFile("temp.zip", "r") as zip_ref:
  31. zip_ref.extractall(destination_folder)
  32. # 删除临时ZIP文件
  33. os.remove("temp.zip")
  34. def copy_file(source_file, destination_file):
  35. # 使用copy2()函数复制文件
  36. shutil.copy2(source_file, destination_file)
  37. def copy_folder(source_folder, destination_folder):
  38. # 使用copytree()函数复制文件夹及其内容
  39. shutil.copytree(source_folder, destination_folder)
  40. def get_chrome_version():
  41. version = "115"
  42. if sys.platform == "win32":
  43. version_re = re.compile(r"^[1-9]\d*\.\d*.\d*")
  44. try:
  45. key = winreg.OpenKey(
  46. winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon"
  47. )
  48. _v, type = winreg.QueryValueEx(key, "version")
  49. return version_re.findall(_v)[0][:3]
  50. except WindowsError as e:
  51. print("check Chrome failed:{}".format(e))
  52. else:
  53. return version
  54. chrome_version = get_chrome_version() # 要更新的chromedriver版本
  55. print("Detected your chrome version is: ", chrome_version)
  56. chrome_driver_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
  57. win64_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  58. win32_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  59. mac_chrome_path = "/Applications/Google Chrome.app"
  60. linux_chrome_path = "/opt/google/chrome"
  61. old_driver_version = {
  62. "100":"100.0.4896.60",
  63. "101":"101.0.4951.41",
  64. "102":"102.0.5005.61",
  65. "103":"103.0.5060.134",
  66. "104":"104.0.5112.79",
  67. "105":"105.0.5195.52",
  68. "106":"106.0.5249.61",
  69. "107":"107.0.5304.62",
  70. "108":"108.0.5359.71",
  71. "109":"109.0.5414.74",
  72. "110":"110.0.5481.77",
  73. "111":"111.0.5563.64",
  74. "112":"112.0.5615.49",
  75. "113":"113.0.5672.63",
  76. "114":"114.0.5735.90",
  77. }
  78. if __name__ == "__main__":
  79. driver_downloads = []
  80. response = requests.get(chrome_driver_url)
  81. if response.status_code == 200:
  82. versions = json.loads(response.content)["versions"]
  83. versions = versions[::-1] # 倒序排列数组
  84. for info in versions:
  85. version = info["version"]
  86. if version.find(chrome_version) >= 0:
  87. downloads = info["downloads"]
  88. if "chromedriver" in downloads:
  89. print(info["version"])
  90. driver_downloads = downloads["chromedriver"]
  91. break
  92. else:
  93. print("Error: " + response.status_code)
  94. exit(1)
  95. if not driver_downloads and int(chrome_version) < 115:
  96. if chrome_version not in old_driver_version:
  97. print("没有可用的chromedriver")
  98. exit(1)
  99. full_version = old_driver_version[chrome_version]
  100. driver_downloads = [
  101. {
  102. "platform": "linux64",
  103. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_linux64.zip",
  104. },
  105. {
  106. "platform": "mac-arm64",
  107. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac_arm64.zip",
  108. },
  109. {
  110. "platform": "mac-x64",
  111. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac64.zip",
  112. },
  113. {
  114. "platform": "win32",
  115. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
  116. },
  117. {
  118. "platform": "win64",
  119. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
  120. },
  121. ]
  122. if os.path.exists("./chromedrivers"):
  123. shutil.rmtree("./chromedrivers")
  124. os.mkdir("./chromedrivers")
  125. if sys.platform == "win32" and platform.architecture()[0] == "64bit":
  126. for download in driver_downloads:
  127. if download["platform"] == "win64":
  128. url = download["url"]
  129. print("ChromeDriver will be downloaded from: ", url)
  130. break
  131. download_and_extract_zip(url, "./chromedrivers")
  132. if os.path.exists("./chrome_win64"):
  133. shutil.rmtree("./chrome_win64")
  134. copy_folder(win64_chrome_path, "./chrome_win64")
  135. for folder in os.listdir("./chrome_win64"):
  136. if folder[0].isdigit() and os.path.isdir("./chrome_win64/"+folder):
  137. shutil.rmtree("./chrome_win64/"+folder+"/Installer") # 删除Installer文件夹
  138. copy_file("./execute_win64.bat", "./chrome_win64/execute.bat")
  139. copy_file("./stealth.min.js", "./chrome_win64/stealth.min.js")
  140. try:
  141. copy_file(
  142. "./chromedrivers/chromedriver-win64/chromedriver.exe",
  143. "./chrome_win64/chromedriver_win64.exe",
  144. )
  145. except:
  146. copy_file(
  147. "./chromedrivers/chromedriver.exe",
  148. "./chrome_win64/chromedriver_win64.exe",
  149. )
  150. finally:
  151. shutil.rmtree("./chromedrivers")
  152. elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
  153. for download in driver_downloads:
  154. if download["platform"] == "win32":
  155. url = download["url"]
  156. print("ChromeDriver will be downloaded from: ", url)
  157. break
  158. download_and_extract_zip(url, "./chromedrivers")
  159. if os.path.exists("./chrome_win32"):
  160. shutil.rmtree("./chrome_win32")
  161. copy_folder(win64_chrome_path, "./chrome_win32")
  162. for folder in os.listdir("./chrome_win32"):
  163. if folder[0].isdigit() and os.path.isdir("./chrome_win32/"+folder):
  164. shutil.rmtree("./chrome_win32/"+folder+"/Installer") # 删除Installer文件夹
  165. copy_file("./execute_win32.bat", "./chrome_win32/execute.bat")
  166. copy_file("./stealth.min.js", "./chrome_win32/stealth.min.js")
  167. try:
  168. copy_file(
  169. "./chromedrivers/chromedriver-win32/chromedriver.exe",
  170. "./chrome_win32/chromedriver_win32.exe",
  171. )
  172. except:
  173. copy_file(
  174. "./chromedrivers/chromedriver.exe",
  175. "./chrome_win32/chromedriver_win64.exe",
  176. )
  177. finally:
  178. shutil.rmtree("./chromedrivers")
  179. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  180. pass
  181. elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  182. processor = get_processor_info()
  183. if processor == "Intel":
  184. driver_arch = "mac-x64"
  185. elif processor == "Apple":
  186. driver_arch = "mac-arm64"
  187. for download in driver_downloads:
  188. if download["platform"] == driver_arch:
  189. url = download["url"]
  190. print("ChromeDriver will be downloaded from: ", url)
  191. break
  192. download_and_extract_zip(url, "./chromedrivers")
  193. if os.path.exists("./chrome_mac64.app"):
  194. shutil.rmtree("./chrome_mac64.app")
  195. # copy_folder(mac_chrome_path, "./chrome_mac64.app")
  196. subprocess.call(["cp", "-R", mac_chrome_path, "./chrome_mac64.app"])
  197. try:
  198. copy_file(
  199. "./chromedrivers/chromedriver-%s/chromedriver" % driver_arch,
  200. "./chromedriver_mac64",
  201. )
  202. except:
  203. copy_file(
  204. "./chromedrivers/chromedriver",
  205. "./chromedriver_mac64",
  206. )
  207. finally:
  208. shutil.rmtree("./chromedrivers")
  209. os.chmod("./chromedriver_mac64", 0o755)
  210. os.chmod("./chrome_mac64.app", 0o555)
  211. os.chmod("./chrome_mac64.app/Contents/MacOS/Google Chrome", 0o555)
  212. print("Done and don't forget to generate executestage EXEcutable program!")