gpt4 book ai didi

python - Docker:使用带有 headless Selenium Chromedriver 的容器

转载 作者:太空狗 更新时间:2023-10-30 00:49:25 25 4
gpt4 key购买 nike

我正在尝试链接 peroumal1's "docker-chrome-selenium" container到另一个带有使用 Selenium 的抓取代码的容器。

他将他的容器公开到端口 4444(Selenium 的默认端口),但我无法从我的 scraper 容器访问它。这是我的 docker-compose文件:

chromedriver:
image: eperoumalnaik/docker-chrome-selenium:latest

scraper:
build: .
command: python manage.py scrapy crawl general_course_content
volumes:
- .:/code
ports:
- "8000:8000"
links:
- chromedriver

这是我的抓取工具 Dockerfile:

FROM python:2.7

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /code/

但是,当我尝试在我的代码中使用 Selenium 时(见下文),我收到以下错误消息:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be available in the path. Please look at http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver .在 Mac OS X 上,当我不使用 Docker 时,我通过下载 chromedriver binary 修复了这个问题。并将其添加到路径中,但我不知道在这里做什么。

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://google.com')
driver.close()

编辑:我也在尝试用 Selenium's official images 来做这件事不幸的是,它也不起作用(出现了要求 chromedriver 二进制文件的相同错误消息)。

是否需要对 Python 代码做些什么?

谢谢!

更新:正如@peroumal1 所说,问题是我没有连接到 remote driver using Selenium .但是,在我这样做之后,我遇到了连接问题( urllib2.URLError: <urlopen error [Errno 111] Connection refused> ),直到我修改了 Selenium 驱动程序连接到的 IP 地址(使用 boot2docker 时,您必须连接到虚拟机的 IP 而不是计算机的本地主机,这您可以通过输入 boot2docker ip 找到)并更改 docker-compose文件。这就是我最终得到的:

chromedriver:
image: selenium/standalone-chrome
ports:
- "4444:4444"

scraper:
build: .
command: python manage.py scrapy crawl general_course_content
volumes:
- .:/code
ports:
- 8000:8000
links:
- chromedriver

Python代码(boot2docker在我电脑上的IP地址是192.168.59.103):

driver = webdriver.Remote(
command_executor='http://192.168.59.103:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver.maximize_window()
driver.get('http://google.com')
driver.close()

最佳答案

我认为这里的问题可能不是 Docker,而是代码。Selenium 图像通过远程 Webdriver 为 Selenium 服务器提供接口(interface),所提供的代码尝试使用 chromedriver 直接实例化 Chrome 浏览器,如果 chromedriver 可以从环境中访问,Selenium Python 绑定(bind)可以实现这一点。

也许使用 docs 中的示例会更好:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)

关于python - Docker:使用带有 headless Selenium Chromedriver 的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29781266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com