Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议:关于chrome/driver自动补全的建议 #42

Open
justghostof opened this issue Nov 8, 2020 · 0 comments
Open

建议:关于chrome/driver自动补全的建议 #42

justghostof opened this issue Nov 8, 2020 · 0 comments

Comments

@justghostof
Copy link

直接上代码吧

def Schedule_cmd(blocknum, blocksize, totalsize):
    speed = (blocknum * blocksize) / (time.time() - start_time)
    # speed_str = " Speed: %.2f" % speed
    speed_str = " Speed: %s" % format_size(speed)
    recv_size = blocknum * blocksize

    # 设置下载进度条
    f = sys.stdout
    pervent = recv_size / totalsize
    percent_str = "%.2f%%" % (pervent * 100)
    n = round(pervent * 50)
    s = ('#' * n).ljust(50, '-')
    f.write(percent_str.ljust(8, ' ') + '[' + s + ']' + speed_str)
    f.flush()
    # time.sleep(0.1)
    f.write('\r')


def Schedule(blocknum, blocksize, totalsize):
    speed = (blocknum * blocksize) / (time.time() - start_time)
    # speed_str = " Speed: %.2f" % speed
    speed_str = " Speed: %s" % format_size(speed)
    recv_size = blocknum * blocksize

    # 设置下载进度条
    f = sys.stdout
    pervent = recv_size / totalsize
    percent_str = "%.2f%%" % (pervent * 100)
    n = round(pervent * 50)
    s = ('#' * n).ljust(50, '-')
    print(percent_str.ljust(6, ' ') + '-' + speed_str)
    f.flush()
    time.sleep(2)
    # print('\r')


# 字节bytes转化K\M\G
def format_size(bytes):
    try:
        bytes = float(bytes)
        kb = bytes / 1024
    except:
        print("传入的字节格式不对")
        return "Error"
    if kb >= 1024:
        M = kb / 1024
        if M >= 1024:
            G = M / 1024
            return "%.3fG" % (G)
        else:
            return "%.3fM" % (M)
    else:
        return "%.3fK" % (kb)


def download(url, save_as=None):
    print(f"正在下载{url}")
    if save_as is None:
        save_as = url.split("/")[-1]
    urllib.request.urlretrieve(url=url, filename=save_as,
                                       reporthook=Schedule_cmd)
    
    return save_as
def decompress(file, remove=True):
    shutil.unpack_archive(file)
    if remove:
        os.remove(file)
    print(f"{file}解压完毕")
start_time = time.time()

print("检查Chrome/Chrome-driver")
if platform.system() == "Linux" and os.path.exists("/etc/debian_version"):
    prefix = "sudo " if shutil.which("sudo") else ""
    if shutil.which("chromium-browser") is None:
        os.system(f"{prefix}apt -y install chromium-browser")
    if shutil.which("chromedriver") is None:
        os.system(f"{prefix}apt -y install chromium-chromedriver")
        os.system(f"{prefix}ln -s /usr/lib/chromium-browser/chromedriver /usr/bin")
elif platform.system() == "Linux" and os.path.exists("/etc/redhat-release"):
    prefix = "sudo " if shutil.which("sudo") else ""
    if shutil.which("chromium-browser") is None:
        os.system(f"{prefix}yum -y install chromium")
    if shutil.which("chromedriver") is None:
        os.system(f"{prefix}yum -y install chromedriver")
elif platform.system() == "Windows":
    if not os.path.exists("chrome-win\\chrome.exe"):
        decompress(download("https://npm.taobao.org/mirrors/chromium-browser-snapshots/Win/706915/chrome-win.zip"))
    if not os.path.exists("chromedriver.exe"):
        decompress(download("https://npm.taobao.org/mirrors/chromedriver/79.0.3945.36/chromedriver_win32.zip"))
options = webdriver.ChromeOptions()
if platform.system() == "Linux":
    options.add_argument("no-sandbox")
if platform.system() == "Windows":
    options.binary_location = "chrome-win\\chrome.exe"
driver = webdriver.Chrome(executable_path="chromedriver.exe" if platform.system() == "Windows" else "chromedriver", options=options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant