目录
前言
下载完phantomjs之后直接解压就可以使用,然后在path目录加入phantomjs的路径(以便直接在命令行就可以执行phantomjs命令)。 接下来要完成个代码,一个是用phantomjs去获取页面(采用js编写行为),一个是采用java去调用phantomjs来达到获取内容的作用,接下来直接贴代码。
代码
import java.io.*;
/**
* Created with IntelliJ IDEA.
* User: lsz
* Date: 14-4-22
* Time: 下午1:17
* utils for http
*/
public class HttpUtils {
public static String getAjaxCotnent(String url) throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("phantomjs.exe c:/phantomjs/codes.js "+url);//这里我的codes.js是保存在c盘下面的phantomjs目录
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);
}
//System.out.println(sbf.toString());
return sbf.toString();
}
public static void main(String[] args) throws IOException {
getAjaxCotnent("http://www.oicqzone.com");
}
}