您现在的位置是:首页 > 文章详情

在docker中远程调用PhantomJS

日期:2018-08-11点击:446

背景:写了一个爬虫程序使用的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 

参考:

https://hub.docker.com/r/wernight/phantomjs/

原文链接:https://yq.aliyun.com/articles/633441
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章