簡體   English   中英

如何在 Linux 服務器上使用 docker-compose 安裝 phantomjs 和 selenium?

[英]How to install phantomjs and selenium with docker-compose on linux server?

我正在為我的網絡抓取工具使用 selenium 和 phantomjs。 一切都適用於我的測試 Windows 應用程序。 嘗試將此代碼更新添加到我的主應用程序中,使用 docker-compose 部署,我得到了這個: selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.

我應該如何解決這個問題? 目前我的 docker-compose.yml 有這個代碼:

version: '3.1'

services:

  tgbot:
    container_name: bot
    build:
      context: .
    command: python app.py
    restart: always
    environment:
      WEBAPP_PORT: 3001
    env_file:
      - ".env"
    # bot start after load db
    ports:
      - 8443:3001
    networks:
      - botnet

  phantomjs:
    image: shufo/phantomjs
    command: --webdriver 8901

networks:
      botnet:
        driver: bridge

還有我的python代碼:

from selenium import webdriver
driver = webdriver.PhantomJS()

文件:

FROM python:latest

RUN mkdir /src
WORKDIR /src
COPY requirements.txt /src
RUN pip install -r requirements.txt
COPY . /src

PS我正在使用phantomjs,因為我正在抓取的網頁有JS。 不適用於 chrome

您的配置有幾個問題:

  1. 您的機器人代碼在不同的容器中工作。 不是在啟動 phantomjs 的那個。 這就是它找不到可執行文件的原因。
  2. 您運行的 phantomjs 容器與您的代碼不在同一個網絡中
  3. 有一些無用的配置似乎是從其他示例中復制粘貼的。
  4. 您強制您的容器重新啟動。 即使在成功退出代碼后,它也會重新啟動。

所以這是如何運行所有內容的完整示例:

  1. 創建空文件夾myfolder並將app.py放在那里,內容如下:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(command_executor='http://phantomjs:8901/wd/hub/',desired_capabilities=DesiredCapabilities.PHANTOMJS)
  1. 將 requirements.txt 文件放入myfolder
  2. 將以下Dockerfile放入myfolder
FROM python:latest

WORKDIR /configs
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
  1. 將以下docker-compose.yml放入myfolder
version: '3.1'

services:

  tgbot:
    build: .
    container_name: bot
    volumes:
      - .:/apps
    command: python /apps/app.py
    depends_on:
      - phantomjs
    networks:
      - botnet

  phantomjs:
    container_name: phantomjs
    image: shufo/phantomjs
    command: --webdriver 8901
    networks:
      - botnet

networks:
  botnet:
    driver: bridge
  1. cd myfloder , cd myfloder docker-compose up

輸出:

phantomjs    | [INFO  - 2020-11-10T15:18:11.049Z] GhostDriver - Main - running on port 8901
phantomjs    | [INFO  - 2020-11-10T15:18:11.425Z] Session [f2091fe0-2367-11eb-bcd7-956b9cd40e54] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
phantomjs    | [INFO  - 2020-11-10T15:18:11.425Z] Session [f2091fe0-2367-11eb-bcd7-956b9cd40e54] - page.customHeaders:  - {}
phantomjs    | [INFO  - 2020-11-10T15:18:11.425Z] Session [f2091fe0-2367-11eb-bcd7-956b9cd40e54] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
phantomjs    | [INFO  - 2020-11-10T15:18:11.425Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: f2091fe0-2367-11eb-bcd7-956b9cd40e54
bot exited with code 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM