首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在linux服务器上安装带有的幻影和selenium?

如何在linux服务器上安装带有的幻影和selenium?
EN

Stack Overflow用户
提问于 2020-11-10 12:22:34
回答 1查看 751关注 0票数 0

我正在使用硒和幻影作为我的网络刮刀。所有的工作与我的测试窗口应用程序。尝试将此代码更新添加到我的主应用程序中,该应用程序与docker-组合一起部署,我得到以下内容:selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.

我该怎么解决这个问题?目前,我的docker-compose.yml有以下代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
from selenium import webdriver
driver = webdriver.PhantomJS()

码头文件:

代码语言:javascript
复制
FROM python:latest

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

我使用幻影,因为我正在抓取的网页有JS。铬不起作用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-10 14:11:02

您的配置有几个问题:

  1. 您的机器人代码在不同的容器中工作。不是那个发射幻影的。这就是为什么它找不到executable.
  2. You运行的幻影is容器,它不在您的代码
  3. 的同一网络中,有一些无用的信任似乎是从其他示例复制的。
  4. 强制您的容器重新启动。即使在成功退出代码之后,它也将重新启动。

,下面是如何运行所有内容的完整示例:

  1. 创建空文件夹myfolder,并将包含以下内容的app.py放在那里:

代码语言:javascript
复制
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)

myfolder

  • Put

  • 将requirements.txt文件放在Dockerfilemyfolder

之后

代码语言:javascript
复制
FROM python:latest

WORKDIR /configs
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

myfolder

  1. 将以下docker-compose.yml放置到

代码语言:javascript
复制
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 myfloderdocker-compose up

输出:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64768864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档