java语言 Linux系统环境下 运行phantomjs

安装就不说了,自己百度
控制语句进行如下例句的拼接就行了

/tomcat/phantomjs/linux/bin/phantomjs 
/tomcat/webapps/ROOT/WEB-INF/classes/ofo/js/ofoTokenUtil.js 
http://www.baidu.com
15066668888
1234

第一行表示:你phantomjs安装的绝对路径
第二行表示:你要执行的js语句,本人的js功能是为了拿登录token
第三行表示:你要打开的网站
第四、第五行:传入的参数
注意:每行之间都用空格隔开。

鉴于有些人比较懒:

/**
     * @Author: Lee
     * @Time: 2017/8/15
     * @Description: 调用phantomjs程序,并传入js文件,并通过流拿回需要的数据。
     * @修改人:
     * @修改时间:
     */
    public static String getParsedHtml(String url, Integer pageSize) throws IOException {

        // 如果要更换运行环境,请注意exePath最后的phantom.exe需要更改。因为这个只能在window版本上运行。
        // 前面的路径名也需要和exePath里面的保持一致。否则无法调用
        /**
         * 从uu.prop中获取phantomjs的绝对路径,若phantomjs安装路径有变,请及时更新路径信息
         */

        //本地获取
//        InputStream in =  new BufferedInputStream(new FileInputStream("uu.prop"));
//        Properties prop = new Properties();
//        prop.load(in);
//        String projectPath = prop.getProperty("phantomjsPath");
//        in.close();

        /**
         * 在Tomcat服务器下获取
         */
//        InputStream in = BiddingService.class.getResourceAsStream("/uu.prop");
//        Properties prop = new Properties();
//        prop.load(in);
//        projectPath = prop.getProperty("phantomjsPath");
//        in.close();

        //优化路径获取方法
        log.info("调用apply()里面的Const.getConfig()之前:");
        projectPath = Const.getConfig("config","phantomjsPath");
        log.info(projectPath);


        //String projectPath = System.getProperty("user.dir");本机运行可以释放这句话
        //String projectPath = "D:/mavenProject/wnadmin";

        String sysName = System.getProperty("os.name");//获取当前主机系统名称
        String jsPath = projectPath + File.separator + "web" + File.separator + "phantonjsUtil" + File.separator + "spider"
                + File.separator + "extractUtil.js";

        Runtime rt = Runtime.getRuntime();
        Process p = null;
        if (sysName.contains("Windows")){
            String exePath = projectPath + File.separator + "phantomjs" + File.separator + "windows" + File.separator+ "bin"
                    + File.separator + "phantomjs.exe";
            p = rt.exec(exePath + " " + jsPath + " " + url + " " + pageSize);
        }else if (sysName.contains("OS")){
            //mac运行环境下的路径,如果是mac系统
            String macPath = projectPath + File.separator + "phantomjs" + File.separator + "mac" + File.separator + "bin"
                    + File.separator + "phantomjs";
            p = rt.exec(macPath + " " + jsPath + " " + url + " " + pageSize);
        }else if (sysName.contains("Linux")){
            String linuxPath = projectPath + File.separator + "phantomjs" + File.separator + "linux" + File.separator + "bin"
                    + File.separator + "phantomjs";
            p = rt.exec(linuxPath + " " + jsPath + " " + url + " " + pageSize);
        }else {
            return "暂不支持本机的运行系统";
        }

        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);
        }
        return sbf.toString();
    }

想做什么,对返回的string串做处理吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值