環(huán)境配置
#!/usr/bin/env python
# coding: utf-8
import os;os.system('pip install requests')
try:
? ? import urllib3
? ? if urllib3.__version__ != "1.25.3":os.system('pip uninstall urllib3 -y');os.system('pip install urllib3==1.25.3')
except ModuleNotFoundError:os.system('pip install urllib3==1.25.3')
except ImportError:os.system('pip uninstall urllib3 -y');os.system('pip install urllib3==1.25.3')
os.system('pip install bs4');os.system('pip install lxml');requests = __import__("requests");bs = __import__("bs4").BeautifulSoup;import zipfile;import time;import sys;import re;import winreg
def xml_to_py(xmltext):
? ? soup = bs(xmltext, "lxml");contents_list = []
? ? for i in soup.find_all("contents"):
? ? ? ? item_dick = {}
? ? ? ? for j in i.children:
? ? ? ? ? ? item_dick[j.name] = j.text
? ? ? ? contents_list.append(item_dick)
? ? return contents_list
_v, type = winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon'), 'version');print("當(dāng)前版本號(hào):{}".format(_v));LATEST_RELEASE = _v;req = requests.get("http://chromedriver.storage.googleapis.com/LATEST_RELEASE_{}".format(LATEST_RELEASE.split(".")[0]));LATEST_RELEASE = req.text;print("可用版本號(hào):{}".format(req.text));req = requests.get("http://chromedriver.storage.googleapis.com/?delimiter=/&prefix={}/".format(req.text));contents_list = xml_to_py(req.text);This_LATEST_RELEASE = 0;flag = 0
for i in contents_list:
? ? if sys.platform == re.search(r"_.*", i['key']).group()[1:-4]:This_LATEST_RELEASE = flag;break;
? ? flag += 1
else:
? ? print("編號(hào)\t版本")
? ? for i in contents_list:print(f"{flag}\t{i['key']}");
? ? print("未找到當(dāng)前操作系統(tǒng)版本,請(qǐng)手動(dòng)選擇\n當(dāng)前操作系統(tǒng):{}".format(sys.platform));This_LATEST_RELEASE = int(input("請(qǐng)選擇下載版本編號(hào):"))
print("下載中");req = requests.get("http://chromedriver.storage.googleapis.com/{}".format(contents_list[This_LATEST_RELEASE]['key']));tempname = f"f{int(time.time())}.zip";print("下載成功,進(jìn)行解壓")
with open("{}".format(tempname), "wb")as f:f.write(req.content)
myzip = zipfile.ZipFile(f"{tempname}", "r")
if "chromedriver.exe" in myzip.namelist():myzip.extract("chromedriver.exe", "{}".format('/'.join(sys.executable.split('\\')[:-1])));print("已解壓至{}{}".format("{}".format('/'.join(sys.executable.split('\\')[:-1])),"chromedriver.exe"))
myzip.close();os.remove(tempname);os.system('pip install selenium==3.9.0')