Loading... # 一、安装docker centos系统: ``` yum -y install yum-utils ``` ``` yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo ``` ``` yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin ``` # 二、安装docker-compose ``` yum -y install epel-release yum -y install docker-compose ``` # 三、yaml文件启动selenium docker-compose.yaml ```yaml # To execute this docker-compose yml file use `docker-compose -f docker-compose-v3.yml up` # Add the `-d` flag at the end for detached execution # To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3.yml down` version: "3" services: selenium-hub: image: selenium/hub:4.1.3-20220405 container_name: selenium-hub ports: - "4792:4444" restart: always chrome1: image: selenium/node-chrome:4.1.3-20220405 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 restart: always chrome2: image: selenium/node-chrome:4.1.3-20220405 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 restart: always chrome3: image: selenium/node-chrome:4.1.3-20220405 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 restart: always #edge: # image: selenium/node-edge:4.1.3-20220405 # shm_size: 2gb # depends_on: # - selenium-hub # environment: # - SE_EVENT_BUS_HOST=selenium-hub # - SE_EVENT_BUS_PUBLISH_PORT=4442 # - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 #firefox: # image: selenium/node-firefox:4.1.3-20220405 # shm_size: 2gb # depends_on: # - selenium-hub # environment: # - SE_EVENT_BUS_HOST=selenium-hub # - SE_EVENT_BUS_PUBLISH_PORT=4442 # - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 ``` # 四、运行 ``` docker-compose up -d ``` # 浏览器访问 ``` http://服务器ip:4792 ``` # 实例化方法 ```python #一般的浏览器 def Driver(): options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') print("启动浏览器") driver = webdriver.Remote(command_executor="http://服务器ip:4792/wd/hub", options=options) with open('./stealth.min.js') as f: js = f.read() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": js }) driver.get("https://www.google.com") return driver # 手机模式 def bro_Chrom(): options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--disable-gpu') # # 定义手机版模式 mobile_emulation = { "deviceMetrics": {"width": 390, "height": 844, "pixelRatio": 3.0}, # 定义设备高宽,像素比 "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) " # 通过UA来模拟 "AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"} options.add_experimental_option('mobileEmulation', mobile_emulation) driver = webdriver.Remote(command_executor="http://服务器ip:4792/wd/hub", options=options) with open('./stealth.min.js') as f: js = f.read() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": js }) return driver ``` 最后修改:2024 年 07 月 18 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏