【问题标题】:Running Webdriver in Online Python Plaform( Jupyter lab online, Google Colab etc.)在在线 Python 平台(Jupyterlab online、Google Colab 等)中运行 Webdriver
【发布时间】:2021-10-27 02:50:43
【问题描述】:

我可以在我的本地 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/')

I get the following error:

---------------------------------------------------------------------------
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。

【问题讨论】:

    标签: python selenium web-scraping webdriver google-colaboratory


    【解决方案1】:

    要在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/')
    

    【讨论】:

    • 您好,感谢您的评论。我之前曾在 collab 中尝试过上述代码,但希望在 GUI 中输出而不是无头。您能否建议如何在 colab 中处理 GUI?
    • @NikhilShukla :到目前为止,协作仅限于在无头模式下运行,它不会在 GUI 中运行。
    猜你喜欢
    • 2021-11-04
    • 2020-03-20
    • 2018-11-01
    • 2018-08-31
    • 2022-12-14
    • 1970-01-01
    • 2021-08-12
    • 2021-08-20
    • 2020-09-26
    相关资源
    最近更新 更多