卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章75965本站已运行4317

硒 python 和 docker

硒 python 和 docker

嗨,我展示了如何在 docker 容器中使用 selenium (或 undetected_chromedriver)。


我——dockerfile

我正在使用 docker python 图像并添加 chromdriver 和 chromium 来浏览网站。

第一步是创建requirements.txt 文件。就我个人而言,我使用 undetected-chromedriver 库,它需要 selenium

undetected-chromedriver==3.5.5
from python:3.10



copy ../.. .


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 update
run apt-get install -y chromium

# install chromedriver
run apt-get install -yqq unzip
run wget -o /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -ss chromedriver.storage.googleapis.com/latest_release`/chromedriver_linux64.zip
run unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
env display=:99


run pip install -r requirements.txt


cmd python -u app.py 

例如,您可以在 docker-compose 中运行此 dockerfile。

services:
  bot:
    build: selenium-test

ii - 脚本

在第二步中,我需要添加两个在容器中工作的选项。

我补充:

立即学习“Python免费学习笔记(深入)”;

  • --无沙箱
  • --禁用-setuid-sandbox

这是一个使用 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")
卓越飞翔博客
上一篇: 二叉树后序遍历
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏