python - 在在线 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。
解决方案
要在 中运行脚本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/')
推荐阅读
- prometheus - 普罗米修斯警报.rules; 如何按多个职位筛选?
- php - 为什么ffmpeg php转换0字节为空
- python-2.7 - 如何使用 bs4 将我的网络抓取过程添加到 Python 中的 selenium 自动化中,使其成为一个只要求输入邮政编码的单一过程?
- .net-core - 如何在不对整个解决方案运行 pack 的情况下递归地打包 .NET Core 项目?
- powerapps - Save progress of users in powerapps and it doesn't start with new instance everytime same user opens the app
- java - 将方法作为参数传递给Java中的另一个方法
- jquery - filtering table using two columns
- javascript - Firefox Javascript api extension for topsites thumbs
- java - Java 上的 (Graphics g, Graphics2D) 问题
- data-structures - 从队列或优先队列中删除所有 n 个元素是否更快?