当前位置:   article > 正文

selenium 自动下载120版本以上的chromedriver最新版本_chromedriver 118下载

chromedriver 118下载
  1. import requests, json, os, shutil
  2. from lxml.etree import HTML
  3. # 所有版本驱动的路径 https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
  4. def upzip_file(zip_path=None, unzip_path=None):
  5. """
  6. :zip_path 压缩文件路径
  7. :unzip_path 解压文件路径
  8. :return 解压 zip 文件,返回所有解压文件夹下的路径
  9. """
  10. import zipfile
  11. zip_file = zipfile.ZipFile(zip_path)
  12. if not os.path.isdir(unzip_path):
  13. os.mkdir(unzip_path)
  14. for names in zip_file.namelist():
  15. zip_file.extract(names, unzip_path)
  16. zip_file.close()
  17. return [os.path.join(unzip_path, i).replace('\\', '/') for i in zip_file.namelist()]
  18. def down_chromedriver_zip(urll=None, path=None, version=None):
  19. headers = {
  20. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
  21. }
  22. res1 = requests.get(urll, headers=headers)
  23. response1 = json.loads(res1.text)
  24. for i in response1:
  25. if 'chromedriver_win32' in i['name']:
  26. zip_url = i['url']
  27. name = zip_url.split('/')[-1]
  28. zip_path = os.path.join(path, name)
  29. res2 = requests.get(zip_url, headers=headers)
  30. with open(zip_path, 'wb') as f:
  31. f.write(res2.content)
  32. uzip_path = zip_path.replace('.zip', '')
  33. paths = upzip_file(zip_path, uzip_path)
  34. for chromedriver_path in paths:
  35. if not chromedriver_path.endswith('.exe'):
  36. if os.path.exists(chromedriver_path):
  37. os.remove(chromedriver_path)
  38. continue
  39. os.rename(chromedriver_path, chromedriver_path.replace('.exe', '') + f'_{version}.exe')
  40. if os.path.exists(zip_path):
  41. os.remove(zip_path)
  42. def get_down_url(chrome_version, headers, platform_name="win64"):
  43. """ 获取chromedriver 驱动压缩文件链接
  44. platform_name = ["linux64","mac-arm64","mac-x64","win32","win64"]
  45. """
  46. import sys
  47. if not platform_name:
  48. platform_name = sys.platform
  49. bit_number = platform.architecture()[0]
  50. url = 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
  51. proxies = {"http": None, "https": None}
  52. res = requests.get(url, headers=headers, proxies=proxies)
  53. response = json.loads(res.text)
  54. down_url = None
  55. versions = []
  56. for ver in response['versions']:
  57. version = ver['version']
  58. if str(chrome_version) not in version:
  59. continue
  60. if not str(version).startswith(str(chrome_version)):
  61. continue
  62. versions.append(version)
  63. if len(versions) > 0:
  64. # versiondict = [[int(i.replace('.', '')), i] for i in versions]
  65. # version_new = sorted(versiondict, key=lambda x: x[0])[-1][1]
  66. # print('version_new:', version_new)
  67. version = versions[-1]
  68. if 'linux' in str(platform_name) and bit_number == '64bit':
  69. down_url = f'https://storage.googleapis.com/chrome-for-testing-public/{version}/linux64/chromedriver-linux64.zip'
  70. elif 'win' in str(platform_name) and bit_number == '32bit':
  71. down_url = f'https://storage.googleapis.com/chrome-for-testing-public/{version}/win32/chromedriver-win32.zip'
  72. elif 'win' in str(platform_name) and bit_number == '64bit':
  73. down_url = f'https://storage.googleapis.com/chrome-for-testing-public/{version}/win64/chromedriver-win64.zip'
  74. else:
  75. down_url = f'https://storage.googleapis.com/chrome-for-testing-public/{version}/mac-x64/chromedriver-mac-x64.zip'
  76. return down_url
  77. # chrome_paths = ver['downloads']['chrome']
  78. # for i in chrome_paths:
  79. # if i["platform"] == platform:
  80. # down_url = i['url']
  81. # return down_url
  82. return down_url
  83. def get_down_url0(version, headers):
  84. # 只返回了最新版本
  85. url = 'https://googlechromelabs.github.io/chrome-for-testing/#stable'
  86. zip_url = None
  87. try:
  88. res = requests.get(url, headers=headers)
  89. response = HTML(res.text)
  90. trs = response.xpath('//section/div[@class="table-wrapper"]/table//tr')
  91. for tr in trs:
  92. zip_url = tr.xpath('./td/code[contains(text(), "zip")]/text()')
  93. if 'chromedriver-win32' not in ''.join(zip_url):
  94. continue
  95. code = tr.xpath('./td/code[not(contains(text(), "zip"))]/text()')
  96. if '200' not in ''.join(code):
  97. continue
  98. Binary = tr.xpath('./th[1]/code/text()')
  99. if 'chromedriver' not in Binary:
  100. continue
  101. zip_url = zip_url[0]
  102. if str(version) in zip_url:
  103. break
  104. if isinstance(zip_url, list):
  105. zip_url = zip_url[0]
  106. except Exception as e:
  107. print('e1:', e)
  108. zip_url = None
  109. return zip_url
  110. def get_chromedriver_version1(path, version=None):
  111. headers = {
  112. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
  113. }
  114. # zip_url = get_down_url0(version, headers)
  115. zip_url = get_down_url(version, headers)
  116. if not zip_url:
  117. print('获取驱动链接失败')
  118. return
  119. print('zip_url:', zip_url)
  120. try:
  121. name = zip_url.split('/')[-1]
  122. zip_path = os.path.join(path, name)
  123. flag = False
  124. for i in range(5):
  125. try:
  126. res2 = requests.get(zip_url, headers=headers)
  127. print('res2_status_code::', res2.status_code)
  128. flag = True
  129. break
  130. except:
  131. pass
  132. if not flag:
  133. return
  134. with open(zip_path, 'wb') as f:
  135. f.write(res2.content)
  136. uzip_path = zip_path.replace('.zip', '').replace('chromedriver-win32', 'chromedriver_win32')
  137. # uzip_path = './chromedriver_win32'
  138. paths = upzip_file(zip_path, uzip_path)
  139. for chromedriver_path in paths:
  140. if not chromedriver_path.endswith('.exe'):
  141. if os.path.exists(chromedriver_path):
  142. os.remove(chromedriver_path)
  143. continue
  144. # os.rename(chromedriver_path, chromedriver_path.replace('.exe', '') + f'_{version}.exe')
  145. shutil.copy(chromedriver_path, os.path.join(uzip_path, f'chromedriver_{version}.exe'))
  146. os.remove(chromedriver_path)
  147. exe_path = os.path.join(uzip_path, 'chromedriver-win32')
  148. if os.path.exists(exe_path):
  149. os.rmdir(exe_path)
  150. if os.path.exists(zip_path):
  151. os.remove(zip_path)
  152. except Exception as e:
  153. print('e2:', e)
  154. def get_chromedriver_version(path, version0=None):
  155. url2 = 'https://registry.npmmirror.com/-/binary/chromedriver/'
  156. headers = {
  157. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
  158. }
  159. if version0 and int(version0) > 116:
  160. get_chromedriver_version1(path, version0)
  161. return
  162. try:
  163. res = requests.get(url2, headers=headers)
  164. response = json.loads(res.text)
  165. except:
  166. response = {}
  167. versions = []
  168. dic = {}
  169. for i in response:
  170. if not i['name'].split('.')[0].isdigit():
  171. continue
  172. version = int(i['name'].split('.')[0])
  173. if i['name'].startswith(f'{version0}.'):
  174. versions.append({version: i['url']})
  175. if len(dic) == 0:
  176. dic[version] = i['url']
  177. if len(dic) > 0 and version > list(dic.keys())[0]:
  178. dic.pop(list(dic.keys())[0])
  179. dic[version] = i['url']
  180. if len(versions) == 0 and len(dic) > 0:
  181. versions.append(dic)
  182. if len(versions) > 0:
  183. for k, zip_url in versions[-1].items():
  184. try:
  185. down_chromedriver_zip(zip_url, path, k)
  186. except:
  187. pass
  188. if __name__ == '__main__':
  189. get_chromedriver_version('./', 118)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/925995
推荐阅读
相关标签
  

闽ICP备14008679号