首页 > 解决方案 > 在在线 Python 平台中运行 Webdriver(Jupyter lab online、Google Colab 等)

问题描述

我可以在我的本地 python IDE 上运行 Selinium webdriver 来抓取网络数据,但我想在在线 python IDE 上实现它,例如 Jupyuter lab online 或 Google Colab,但是 chromedriver.exe 路径导致在线工具出现问题并且代码没有执行。我在 Jupyter lab online 和 google colab 上有以下代码:

import os
import sys
os.path.dirname(sys.executable)
from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/home/jovyan/demo/chromedriver.exe' )

browser.get('https://www.youtube.com/')

我收到以下错误:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-19-5510b62cb210> in <module>
      1 from selenium import webdriver
----> 2 browser = webdriver.Chrome(executable_path = '/home/jovyan/demo/chromedriver.exe' )
      3 
      4 browser.get('https://www.youtube.com/' class="ansi-blue-fg">)

/srv/conda/envs/notebook/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:

/srv/conda/envs/notebook/lib/python3.7/site-packages/selenium/webdriver/common/service.py in start(self)
     74                                             stdout=self.log_file,
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:
     78             raise

/srv/conda/envs/notebook/lib/python3.7/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    798                                 c2pread, c2pwrite,
    799                                 errread, errwrite,
--> 800                                 restore_signals, start_new_session)
    801         except:
    802             # Cleanup if the child failed starting.

/srv/conda/envs/notebook/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1549                         if errno_num == errno.ENOENT:
   1550                             err_msg += ': ' + repr(err_filename)
-> 1551                     raise child_exception_type(errno_num, err_msg, err_filename)
   1552                 raise child_exception_type(err_msg)
   1553 

OSError: [Errno 8] Exec format error: '/home/jovyan/demo/chromedriver.exe'

如果您有任何解决方案来解决此问题或任何其他 python 平台,请告诉我,因为我的项目的另一部分取决于这个 webdriver。

标签: pythonseleniumweb-scrapingwebdrivergoogle-colaboratory

解决方案


要在 中运行脚本Google colab,我总是使用下面的脚本,到目前为止从未遇到任何问题。它也只会出现在 中headless mode没有 GUI。

你也没有executable path像你在你的帖子中给出的那样。

!pip install selenium
!apt-get update 
!apt install chromium-chromedriver

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
browser =webdriver.Chrome('chromedriver',chrome_options=chrome_options)
browser.get('https://www.youtube.com/')

推荐阅读