硒 python 和 docker
嗨,我展示了如何在 docker 容器中使用 selenium (或 undetected_chromedriver)。
我——dockerfile
我正在使用 docker python 图像并添加 chromdriver 和 chromium 来浏览网站。
第一步是创建requirements.txt 文件。就我个人而言,我使用 undetected-chromedriver 库,它需要 selenium
undetected-chromedriver==3.5.5
from python:3.10copy ../.. .run wget -q -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -run sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'run apt-get -y updaterun apt-get install -y chromium# install chromedriverrun apt-get install -yqq unziprun wget -o /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -ss chromedriver.storage.googleapis.com/latest_release`/chromedriver_linux64.ziprun unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/env display=:99run pip install -r requirements.txtcmd python -u app.py
例如,您可以在 docker-compose 中运行此 dockerfile。
services: bot: build: selenium-test
ii - 脚本
在第二步中,我需要添加两个在容器中工作的选项。
我补充:
立即学习“Python免费学习笔记(深入)”;
这是一个使用 python 的示例
class app: options: uc.chromeoptions driver: uc.chrome def __init__(self): self.options = uc.chromeoptions() self.options.arguments.extend(["--no-sandbox", "--disable-setuid-sandbox"]) self.driver = uc.chrome(headless=true, use_subprocess=false)
然后您可以使用 undetected_chromedriver 作为 selenium,如下所示:
self.driver.execute_script("console.log("Hello")