亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Python 中自動下載適合 Selenium 的 chromedriver

在 Python 中自動下載適合 Selenium 的 chromedriver

慕田峪4524236 2022-12-06 16:34:21
不幸的是,Chromedriver 總是特定于您安裝的 Chrome 版本。因此,當您通過 PyInstaller 將 python 代碼和 chromedriver 打包到 Windows 的可部署 .exe 文件中時,它在大多數情況下都不起作用,因為您將無法在 .exe 文件中擁有所有 chromedriver 版本。任何人都知道如何從網站自動下載正確的 chromedriver 的方法?如果沒有,我會想出一個代碼來下載 zip 文件并將其解壓縮到臨時文件。謝謝!
查看完整描述

5 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

這是另一個解決方案,webdriver_manager不支持。此腳本將下載最新的 chrome 驅動程序版本。


import requests

import wget

import zipfile

import os


# get the latest chrome driver version number

url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'

response = requests.get(url)

version_number = response.text


# build the donwload url

download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"


# download the zip file using the url built above

latest_driver_zip = wget.download(download_url,'chromedriver.zip')


# extract the zip file

with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:

    zip_ref.extractall() # you can specify the destination folder path here

# delete the zip file downloaded above

os.remove(latest_driver_zip)


查看完整回答
反對 回復 2022-12-06
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

這是 Python 的另一種方法 -


import chromedriver_autoinstaller

from selenium import webdriver


opt = webdriver.ChromeOptions()

opt.add_argument("--start-maximized")


chromedriver_autoinstaller.install()

driver = webdriver.Chrome(options=opt)

driver.get('https://stackoverflow.com/')

這里有更多信息


https://pypi.org/project/chromedriver-autoinstaller/


查看完整回答
反對 回復 2022-12-06
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

Webdriver Manager 會為你做這件事。請參考此鏈接https://pypi.org/project/webdriver-manager/


查看完整回答
反對 回復 2022-12-06
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

我有一個稍微漂亮一點的版本


它會檢測到你的 chrome 版本,獲取正確的驅動程序


def download_chromedriver():

    def get_latestversion(version):

        url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE_' + str(version)

        response = requests.get(url)

        version_number = response.text

        return version_number

    def download(download_url, driver_binaryname, target_name):

        # download the zip file using the url built above

        latest_driver_zip = wget.download(download_url, out='./temp/chromedriver.zip')


        # extract the zip file

        with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:

            zip_ref.extractall(path = './temp/') # you can specify the destination folder path here

        # delete the zip file downloaded above

        os.remove(latest_driver_zip)

        os.rename(driver_binaryname, target_name)

        os.chmod(target_name, 755)

    if os.name == 'nt':

        replies = os.popen(r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version').read()

        replies = replies.split('\n')

        for reply in replies:

            if 'version' in reply:

                reply = reply.rstrip()

                reply = reply.lstrip()

                tokens = re.split(r"\s+", reply)

                fullversion = tokens[len(tokens) - 1]

                tokens = fullversion.split('.')

                version = tokens[0]

                break

        target_name = './bin/chromedriver-win-' + version + '.exe'

        found = os.path.exists(target_name)

        if not found:

            version_number = get_latestversion(version)

            # build the donwload url

            download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"

            download(download_url, './temp/chromedriver.exe', target_name)


    elif os.name == 'posix':

        reply = os.popen(r'chromium --version').read()


        if reply != '':

            reply = reply.rstrip()

            reply = reply.lstrip()

            tokens = re.split(r"\s+", reply)

            fullversion = tokens[1]

            tokens = fullversion.split('.')

            version = tokens[0]

        else:

            reply = os.popen(r'google-chrome --version').read()

            reply = reply.rstrip()

            reply = reply.lstrip()

            tokens = re.split(r"\s+", reply)

            fullversion = tokens[2]

            tokens = fullversion.split('.')

            version = tokens[0]


        target_name = './bin/chromedriver-linux-' + version

        print('new chrome driver at ' + target_name)

        found = os.path.exists(target_name)

        if not found:

            version_number = get_latestversion(version)

            download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_linux64.zip"

            download(download_url, './temp/chromedriver', target_name) 

 


查看完整回答
反對 回復 2022-12-06
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

截至 2021 年 8 月,這是狀態:-


chromedriver-自動安裝程序

自動下載并安裝支持當前安裝的 chrome 版本的 chromedriver。此安裝程序支持 Linux、MacOS 和 Windows 操作系統。


安裝

pip install chromedriver-autoinstaller

用法

Just type import chromedriver_autoinstaller in the module you want to use chromedriver.

例子

from selenium import webdriver

import chromedriver_autoinstaller



chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists

                                      # and if it doesn't exist, download it automatically,

                                      # then add chromedriver to path


driver = webdriver.Chrome()

driver.get("http://www.python.org")

assert "Python" in driver.title


查看完整回答
反對 回復 2022-12-06
  • 5 回答
  • 0 關注
  • 380 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號