简体   繁体   English

OSError:[Errno 8]在Docker容器中的python中运行硒时

[英]OSError: [Errno 8] when running selenium in python in a docker container

I've recently learned the basics of Docker and how to create and run images. 我最近了解了Docker的基础知识以及如何创建和运行映像。 I'm trying to create an image of a python script that scrapes some webpages for data and uploads it to a server. 我正在尝试创建python脚本的图像,该图像会刮擦一些网页以获取数据并将其上传到服务器。 I'm using Selenium, Chromium, and a Windows chromedriver. 我正在使用Selenium,Chromium和Windows chromedriver。 I'm trying to build the image on my Windows machine and be able to deploy it on a bunch of Linux/Windows servers. 我正在尝试在Windows计算机上构建映像,并能够将其部署在Linux / Windows服务器上。 Currently, I'm only building and running on the same Windows machine, just until I get it running, but I keep getting the same error, even though the script runs just fine directly on the machine itself. 目前,我只是在同一台Windows计算机上构建并运行,直到我将其运行为止,但是即使脚本直接在计算机本身上运行得很好,我仍然遇到相同的错误。

This is the error: 这是错误:

Traceback (most recent call last):
  File "my-app.py", line 796, in <module>
    startScraper();
  File "my-app.py", line 92, in startScraper
    browser = webdriver.Chrome(chrome_options = options, executable_path = path_to_chromedriver);
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/usr/local/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.6/subprocess.py", line 1326, in _execute_child
    raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error

It seems to be related to the chrome options but even when I remove all the "add-argument" options, the error persists, but here are the options: 它似乎与chrome选项有关,但是即使我删除了所有的“ add-argument”选项,错误仍然存​​在,但以下是这些选项:

options = webdriver.ChromeOptions();
options.binary_location = './chrome-win32/chrome.exe';
options.add_argument('headless')
options.add_argument('window-size=1400x1300')
options.add_argument('--mute-audio')
options.add_argument('--disable-web-security');
options.add_argument('--allow-running-insecure-content');
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
prefs = {"profile.managed_default_content_settings.images":2}
options.add_experimental_option("prefs", prefs);
path_to_chromedriver = './chromedriver.exe';

Is there anything that I'm missing to be able to run this scraper in a container? 有什么我需要在容器中运行此刮板的东西吗? Thanks! 谢谢!

EDIT: I forgot to add the Dockerfile and how I build/run the image: 编辑:我忘记添加Dockerfile以及如何构建/运行映像:

Dockerfile: Dockerfile:

FROM python:3.6.0

WORKDIR /my-app

ADD . /my-app

RUN pip install -r requirements.txt

ENV NAME Scraper

CMD ["python", "My_App.py"]

Build/Run image: - docker build -t myapp - docker run myapp 构建/运行镜像:-docker build -t myapp-docker run myapp

Maybe there are some options that I don't know about that I'm missing? 也许有些我不知道的选项我不知道了吗?

You are trying to run a exe inside a linux container and that is not going to work. 您正在尝试在linux容器中运行exe文件,但无法正常工作。 You will need to install chrome and chromedriver inside your Dockerfile and update the code to use the correct path 您将需要在Dockerfile中安装chrome和chromedriver并更新代码以使用正确的路径

FROM python:3.6.0
RUN apt update && apt install -y chromedriver
WORKDIR /my-app
ADD . /my-app
RUN pip install -r requirements.txt

ENV NAME Scraper

CMD ["python", "My_App.py"]

Change your code to 将您的代码更改为

options = webdriver.ChromeOptions();
options.add_argument('headless')
options.add_argument('window-size=1400x1300')
options.add_argument('--mute-audio')
options.add_argument('--disable-web-security');
options.add_argument('--allow-running-insecure-content');
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
prefs = {"profile.managed_default_content_settings.images":2}
options.add_experimental_option("prefs", prefs);
path_to_chromedriver = '/usr/lib/chromium/chromedriver';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - Ubuntu中的Selenium OSError:[Errno 20]不是目录 - Python - Selenium in Ubuntu OSError: [Errno 20] Not a directory OSError:[Errno 8] Exec格式错误:&#39;geckodriver&#39;试图在python中使用selenium打开firefox - OSError: [Errno 8] Exec format error: 'geckodriver' when trying to open firefox using selenium in python 在 Raspberry pi 4 上运行 Python3 脚本时如何解决“OSError: [Errno 8] Exec format error”? - How to solve 'OSError: [Errno 8] Exec format error' when running Python3 script on Raspberry pi 4? Python Selenium chromedriver OSError:[Errno 8] Exec格式错误 - Python Selenium chromedriver OSError: [Errno 8] Exec format error OSError:[Errno 8]尝试运行chromedriver for selenium时出现exec格式错误 - OSError: [Errno 8] Exec format error when trying to run chromedriver for selenium 在docker容器中运行firefox时,Selenium Webdriver拒绝连接错误 - Connection refused error by Selenium webdriver when running firefox in a docker container Python OSError:[Errno 2]没有这样的文件err - Python OSError: [Errno 2] No such file err OSError:[Errno 8] Exec格式错误selenium - OSError: [Errno 8] Exec format error selenium 在 python:3.7 容器上运行 Selenium - Running Selenium on python:3.7 Container 在docker容器中运行selenium chrome浏览器 - running selenium chrome browser in docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM