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

来源:互联网 发布:win10安装tensorflow 编辑:程序博客网 时间:2024/05/16 04:57

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

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

第一行表示:你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串做处理吧

原创粉丝点击