日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)

發布時間:2024/9/27 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、? 在使用phantomjs的時候需要下載phantomjs,網上有window版本和Linux版本。將phantomjs放在Linux上的指定位置之后(如下面的/home/tpl/phantomjs-1.9.7-linux-x86_64/處),

2、? 按照類似如下命令進行測試,是否可以生成圖片(執行前檢查phantomjs的權限,要有執行權限):

/home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

/home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js ?????

http://www.baidu.com

/home/test/index.png

注意上面的命令,每個之間用空格隔開,其中:

/home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs表示的是phantomjs所在的位置

/home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js表示使用快照的時候需要的一個js

http://www.baidu.com? 要生成快照的頁面

/home/test/index.png? 表示要將截的圖放在的位置

?

3、要使用程序生成快照,需要執行shell命令或者cmd命令

能夠兼容javawindows的一段java代碼如下:

package xxxxx.webservice.snapshot;

?

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

?

import com.ucap.utils.OsUtils;

?

/**

?* SnapshotServiceImpl.java 用于生成快照的服務類

?*

?* @attention 使用注意事項

?* @author

?* @date

?* @note begin modify by 涂作權 2016-5-19 判斷在Linux上的情況

?*/

public class SnapshotServiceImpl implements SnapshotService {

?

???????? /**

???????? ?* phantomjsPath?? :phantomjs

???????? ?*???? /home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

???????? ?*???? /home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js?

???????? ?*???? http://xxxxx:5555/tpl/special/2016/5/19/2d0cd5d12a974e438a2f4a13300f60d2/index.html

???????? ?*????

???????? ?* exportPath????? :導出路徑,這是一個絕對路徑?

???????? ?*

???????? ?* 關于phantomjs的執行命令如:

???????? ?*???? /home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

???????? ?*???? /home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js

???????? ?*???? http://xxxx:5555/tpl/special/2016/5/19/2d0cd5d12a974e438a2f4a13300f60d2/index.html

???????? ?*???? /home/test/index.png

???????? ?*/

???????? public String createSnapshot(String phantomjsPath, String exportPath) {

?????????????????? URL path = this.getClass().getClassLoader().getResource("/");

?????????????????? String str = path.getPath();

?????????????????? File file = new File(str);

?????????????????? str = file.getParentFile().getParentFile().getParentFile()

???????????????????????????????????? .getAbsolutePath()

???????????????????????????????????? + "/temp" + exportPath;

?

?????????????????? String[] strArray = null;

?

?????????????????? // 判斷linux的執行情況

?????????????????? if (OsUtils.IS_LINUX) {

??????????????????????????? strArray = new String[3];

??????????????????????????? strArray[0] = "/bin/sh";

??????????????????????????? strArray[1] = "-c";

??????????????????????????? strArray[2] = phantomjsPath + " " + str;

?????????????????? } else if (OsUtils.IS_WINDOWS) {

??????????????????????????? strArray = new String[4];

??????????????????????????? strArray[0] = "cmd.exe";

??????????????????????????? strArray[1] = "/C";

??????????????????????????? strArray[2] = phantomjsPath;

??????????????????????????? strArray[3] = str;

?????????????????? }

?

?????????????????? File fileInfo = new File(str);

?????????????????? File fi = new File(fileInfo.getParentFile().getAbsolutePath());

?????????????????? if (!fi.exists()) {

??????????????????????????? fi.mkdirs();

?????????????????? }

?

?????????????????? // windowsphantomjs位置

?????????????????? Runtime rt = Runtime.getRuntime();

?????????????????? Process process = null;

?????????????????? try {

??????????????????????????? process = rt.exec(strArray);

?????????????????? } catch (IOException e) {

??????????????????????????? e.printStackTrace();

?????????????????? }

?????????????????? StringBuffer sbf = null;

?????????????????? InputStream is = null;

?????????????????? try {

??????????????????????????? is = process.getInputStream();

??????????????????????????? BufferedReader br = new BufferedReader(new InputStreamReader(is));

??????????????????????????? sbf = new StringBuffer();

??????????????????????????? String tmp = "";

?

??????????????????????????? while ((tmp = br.readLine()) != null) {

???????????????????????????????????? sbf.append(tmp);

??????????????????????????? }

?????????????????? } catch (IOException e) {

??????????????????????????? e.printStackTrace();

?????????????????? } finally {

??????????????????????????? if (is != null) {

???????????????????????????????????? try {

?????????????????????????????????????????????? is.close();

???????????????????????????????????? } catch (IOException e) {

?????????????????????????????????????????????? e.printStackTrace();

???????????????????????????????????? }

???????????????????????????????????? is = null;

??????????????????????????? }

?????????????????? }

?

?????????????????? return sbf.toString();

???????? }

}

其中一個依賴的test.js如下:

var page = require('webpage').create();

?

//-----------------------------獲取窗口的寬高信息,并設置,write by 涂作權 start------------------------------------

var winWidth = null;

var winHeight = null;

// 獲取窗口寬度

if (window.innerWidth) {

??? winWidth = window.innerWidth;

} else if ((document.body) && (document.body.clientWidth)) {

??? winWidth = document.body.clientWidth;

}

// 獲取窗口高度

if (window.innerHeight) {

??? winHeight = window.innerHeight;

} else if ((document.body) && (document.body.clientHeight)) {

??? winHeight = document.body.clientHeight;

}

// 通過深入 Document 內部對 body 進行檢測,獲取窗口大小

if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {

??? winHeight = document.documentElement.clientHeight;

??? winWidth = document.documentElement.clientWidth;

}

page.viewportSize = {width:winWidth,height:winHeight};

page.clipRect = {top:0,left:0,width:winWidth,height:winHeight};

//-----------------------------獲取窗口的寬高信息,并設置end--------------------------------------

//-----------------------------------------------------------------------------------------------

page.settings = {

???????? javascriptEnabled:false,

???????? loadImages:true,

???????? userAgent:'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0'

};

//-----------------------------------------------------------------------------------------------

?

?

system = require('system');

//var url = 'http://yule.club.sohu.com/gifttrade/thread/2m2efbrpfui';

var address;

var loca;

if(system.args.length == 1){

???????? phantom.exit();

}else{

???????? adress = system.args[1];

???????? loca = system.args[2];

???????? page.open(adress, function (status){

??? if (status != "success"){

??????? console.log('FAIL to load the address');

??????? phantom.exit();

??? }

???????

??? page.evaluate(function(){

??????? //此函數在目標頁面執行的,上下文環境非本phantomjs,所以不能用到這個js中其他變量??????

??????? //window.scrollTo(0,10000);//滾動到底部

?????????????????? //滾動到自適應高度

??????? //window.document.body.scrollTop = document.body.scrollHeight;

???????

?????? /* window.setTimeout(function(){

??????????? var plist = document.querySelectorAll("a");

??????????? var len = plist.length;

??????????? while(len)

??????????? {

??????????????? len--;

??? ????????????var el = plist[len];

??????????????? el.style.border = "1px solid red";

??????????? }

??????? },5000);*/

??? });

???

??? window.setTimeout(function (){

??? ?????????????????? //在本地生成截圖

??????????????????????????? page.render(loca);??????

??????????????????????????? //console.log(loca);

??????????????????????????? phantom.exit();

?????????????????? }, 1000);

???????? });

}

?

總結

以上是生活随笔為你收集整理的phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。