在docker中远程调用PhantomJS
背景:写了一个爬虫程序使用的docker部署,部署后在docker中安装PhantomJS 比较麻烦,于是想到了再另一个docker容器中部署一个PhantomJS ,通过远程调用的方式来使用它
1.启动一个PhantomJS docker实例
[root@Ieat1 ~]# docker run -d --name phantomjs -p 8910:8910 wernight/phantomjs phantomjs --webdriver=8910
2.通过程序远程调用
通过Java 调用:
WebDriver driver = new RemoteWebDriver( new URL("http://127.0.0.1:8910"), DesiredCapabilities.phantomjs());
通过python调用,先运行 pip install selenium安装selenium
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities driver = webdriver.Remote( command_executor='http://127.0.0.1:8910', desired_capabilities=DesiredCapabilities.PHANTOMJS) driver.get('http://example.com') driver.find_element_by_css_selector('a[title="hello"]').click() driver.quit()
3.完整的代码段:
public class RemotePhantomjsTest { public static void main(String[] args) throws InterruptedException { DesiredCapabilities dc = new DesiredCapabilities(); dc.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"); WebDriver webDriver = null; try { webDriver = new RemoteWebDriver(new URL("http://127.0.0.1:8910"), dc); } catch (MalformedURLException e) { e.printStackTrace(); } try { webDriver.get("https://www.baidu.com/"); WebElement webElement = webDriver.findElement(By.xpath("/html")); String content = webElement.getAttribute("outerHTML"); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } finally { webDriver.close(); } } }
监控一下docker的日志,发现调用成功
[root@Ieat1 ~]# docker logs -f phantomjs [INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","webSecurityEnabled":true} [INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - page.customHeaders: - {} [INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - 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.page.settings.userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"} [INFO - 2018-08-12T12:03:20.392Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: b47e2840-9e27-11e8-81d3-1d2649e3289e
参考:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
使用shell脚本监控少量服务器并发送微信告警信息
01. 概括 1.0 脚本更新地址 Git更新地址:shell_monitor_script.sh 文章原文地址:使用shell脚本监控少量服务器并发送微信告警信息 1.1 前提: 平台系统:linux系统下 接收信息:微信企业号 1.2 脚本说明 需要修改微信接口 脚本对应参数的修改:包括IP,告警阈值等 1.3 脚本使用 该脚本监控包括Disk、CPU、MEM、LOAD等主机资源,以及docker服务和docker容器。 将该脚本附件假定放于:/home/mai/.check_host.sh。 那么在定时任务上可以增加定时任务: $ echo "## 每分钟执行监控脚本一次" | sudo tee -a /etc/crontab $ echo "*/1 * * * * mai bash /home/mai/.check_host.sh" | sudo tee -a /etc/crontab 02. 脚本附件 #!/bin/bash #!/bin/bash # Create for mai # Copyright http://www.mairoot.com # Create d...
- 下一篇
ECS查询特权接口DescribeAccountAttributes发布
背景 弹性云服务ECS针对用户查询单个region各种quota限制、按量高配配置及一些特权信息。 说明 DescribeAccountAttributes 接口介绍查询某类资源的使用上限: instance-network-type:当前地域可选择的网络类型。取值: vpc 专有网络 classic 经典网络 max-security-groups:全地域的安全组数量。 max-elastic-network-interfaces:当前地域下弹性网卡的数量。 max-postpaid-instance-vcpu-count:当前地域下按量实例的vCPU核数。 max-spot-instance-vcpu-count:当前地域下抢占式实例vCPU核数。 max-delicated-hosts:当前地域下专用宿主机数量。 supported-postpai
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Mario游戏-低调大师作品
- 2048小游戏-低调大师作品
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案