【问题标题】:Running a .py file with selenium in docker在 docker 中运行带有 selenium 的 .py 文件
【发布时间】:2021-01-25 12:41:37
【问题描述】:

我有一个使用 selenium 抓取一些网络信息的 python 脚本。我已经为我的项目构建了一个 docker 映像:

FROM python:3.7-slim

WORKDIR /

COPY requirements.txt ./

RUN pip install --upgrade pip && pip install -r requirements.txt

COPY . .

RUN pip install -e .

CMD ["python", "src/project/scraper.py"]

运行时出现以下错误:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

chromedriver.exe 文件位于数据文件夹中,.py 脚本引用了正确的位置(它确实在本地运行)。

有人知道我如何在这个容器中运行 chrome 吗? 地图结构如下:

|-- data
|  |--chromedriver.exe
|  |--file.csv
|-- src
|  |--project
|     |--scraper.py
|-- Dockerfile
|-- requirements.txt

谢谢!

【问题讨论】:

    标签: python docker selenium


    【解决方案1】:

    让我分享过去对我有用的东西。

    尝试从 DockerFile 中安装 chrome、chromedriver 和 PATH。


    注意:

    • 使用 Python 3.8。您可以尝试将其更改为 3.7,看看它是否适合您。
    • 未针对多阶段构建进行配置。
      • 即您可能需要在最后附加您的部分之前删除“FROM python:3.7-slim”。
    FROM python:3.8 AS builder
    
    RUN apt-get update; apt-get clean
    
    # Install chrome dependencies
    RUN apt-get install -y x11vnc xvfb fluxbox wget wmctrl unzip
    
    # Set up the Chrome PPA
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
    
    # Update the package list and install chrome
    RUN apt-get update -y
    RUN apt-get install -y google-chrome-stable
    
    # Set up Chromedriver Environment variables
    ENV CHROMEDRIVER_VERSION 87.0.4280.88
    ENV CHROMEDRIVER_DIR /chromedriver
    RUN mkdir $CHROMEDRIVER_DIR
    
    # Download and install Chromedriver
    RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
    RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
    
    # Put Chromedriver into the PATH
    ENV PATH $CHROMEDRIVER_DIR:$PATH
    
    RUN python -m venv /opt/venv
    # Make sure we use the virtualenv:
    ENV PATH="/opt/venv/bin:$PATH"
    
    ...
    <YOUR_DOCKERFILE_PARTS>
    
    

    截至今天(2021 年 1 月 25 日),我可以查看谷歌浏览器的 latest stable version (released Jan 19th, 2021) 是 88.0.4324.96。所以如果上述方法不起作用,请尝试更改Chromedriver version,使其与安装的chrome浏览器匹配。

    【讨论】:

      【解决方案2】:

      我希望您在 Docker 中使用 Linux 容器,因为 python:3.7-slim 是 Linux 映像。您无法在 Linux 中执行 Windows 二进制 (.exe) 文件。因此需要在Linux上安装chromedriver:How to Setup Selenium with ChromeDriver on Ubuntu 18.04 & 16.04

      你的 Dockerfile 应该是这样的

      FROM python:3.7-slim
      
      # install chromedriver
      RUN apt-get update && \
          apt-get install -y unzip xvfb libxi6 libgconf-2-4 && \
          apt-get install default-jdk && \
          curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add && \
         echo "deb [arch=amd64]  http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
         apt-get -y update && \
         wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip && \
         unzip chromedriver_linux64.zip && \
         mv chromedriver /usr/bin/chromedriver && \
         chown root:root /usr/bin/chromedriver && \
         chmod +x /usr/bin/chromedriver    
      
      WORKDIR /
      
      COPY requirements.txt ./
      
      RUN pip install --upgrade pip && pip install -r requirements.txt
      
      COPY . .
      
      RUN pip install -e .
      
      CMD ["python", "src/project/scraper.py"]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 2011-09-30
        • 2017-04-17
        • 2021-10-16
        • 2019-06-18
        相关资源
        最近更新 更多