欢迎光临散文网 会员登陆 & 注册

环境配置

2023-06-09 17:11 作者:想吸莱莱  | 我要投稿

#!/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("当前版本号:{}".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("可用版本号:{}".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("编号\t版本")

    for i in contents_list:print(f"{flag}\t{i['key']}");

    print("未找到当前操作系统版本,请手动选择\n当前操作系统:{}".format(sys.platform));This_LATEST_RELEASE = int(input("请选择下载版本编号:"))

print("下载中");req = requests.get("http://chromedriver.storage.googleapis.com/{}".format(contents_list[This_LATEST_RELEASE]['key']));tempname = f"f{int(time.time())}.zip";print("下载成功,进行解压")

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')


环境配置的评论 (共 条)

分享到微博请遵守国家法律