码迷,mamicode.com
首页 > 编程语言 > 详细

java phantomjs

时间:2018-12-05 20:31:11      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:buffere   tls   gen   loading   OLE   ons   runtime   stringbu   view   

1:下载:phantomjs http://phantomjs.org/download.html

2:JAVA代码

public     void   HttpunitlStr(String url)
{
     HTML="";
    String jsPath = "C:\\phantomjs\\examples\\myjs.js";
    String exePath = "C:\\phantomjs\\bin\\phantomjs.exe";
    System.out.println(jsPath);
    System.out.println(exePath);
    Runtime rt = Runtime.getRuntime();
    Process p;
    try {
        p = rt.exec(exePath + " " + jsPath + " " + url);

    InputStream is = p.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer sbf = new StringBuffer();
    String tmp = "";
    while ((tmp = br.readLine()) != null)
    {
        sbf.append(tmp);
    }
    HTML=sbf.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 
}

 

  

3:js代码

var page = require(‘webpage‘).create(),
  system = require(‘system‘),
  t, address;

page.settings.loadImages = false;  //为了提升加载速度,不加载图片
page.settings.resourceTimeout = 10000;//超过10秒放弃加载
//此处是用来设置截图的参数。不截图没啥用
page.viewportSize = {
  width: 1280,
  height: 800
};
block_urls = [‘google.com‘];//为了提升速度,屏蔽一些需要时间长的。比如百度广告
page.onResourceRequested = function(requestData, request){
    for(url in block_urls) {
        if(requestData.url.indexOf(block_urls[url]) !== -1) {
            request.abort();
           return;
        }
    }            
}
t = Date.now();//看看加载需要多久。
address = system.args[1];
page.open(address, function(status) {
  if (status !== ‘success‘) {
    console.log(‘FAIL to load the address‘);
  } else {
    t = Date.now() - t;

    console.log(‘Loading time ‘ + t + ‘ msec‘);
    console.log(page.content);
    setTimeout(function(){ phantom.exit(); }, 6000);
  }
  phantom.exit();
});

 

java phantomjs

标签:buffere   tls   gen   loading   OLE   ons   runtime   stringbu   view   

原文地址:https://www.cnblogs.com/sobigdata/p/10072683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!