1、下载phantomjs
http://phantomjs.org/
2、运行
新建phantomjs.bat,记得改目录路径
里面内容为:
D:\java\phantomjs\phantomjs.exe D:\java\phantomjs\code\server.js 8080
3、新建server.js文件,放在code目录下,注意code目录也是新建的,以下是server.js内容
if (s=== "success" ) { page.evaluate(function () { $(".canvas_box").appendTo(".app_box").css("float","none").siblings().remove(); $(".app_box,body").css("margin",0); }); var base64= page.renderBase64('png'),type="data:image/png;base64," var img="<img src='"+type+base64+"'>" res.write(img); res.close(); }var page = require('webpage').create(); var server = require('webserver').create(); var sys = require('system'); if(sys.args.length !== 2){ console.log('args is error,demo: phantomjs server.js 80'); phantom.exit(1); }else{ console.log("welcome! system is start on port:"+sys.args[1]+"\ntest url is: http://localhost/http://www.baidu.com"); } var port = sys.args[1]; //输出到网页预览图片,地址:http://localhost/http://www.baidu.com /* service = server.listen(port,function(req, res){ var url=decodeURIComponent(req.url).substr(1); res.statusCode = 200; res.headers = { 'Cache': 'no-cache', 'Content-Type': 'text/html;charset=utf-8' }; page.open(url, function (s) { if (s=== "success" ) { page.evaluate(function () { $(".canvas_box").appendTo(".app_box").css("float","none").siblings().remove(); $(".app_box,body").css("margin",0); }); var base64= page.renderBase64('png'),type="data:image/png;base64," var img="<img src='"+type+base64+"'>" res.write(img); res.close(); } }); });*/ //输出base64字符串 service = server.listen(port,function(req, res){ var url=decodeURIComponent(req.url).substr(1); res.statusCode = 200; res.headers = { 'Cache': 'no-cache', 'Content-Type': 'text/html;charset=utf-8' }; page.open(url,/*{ keepAlive: true },*/ function (s) { var base64= page.renderBase64('png'); res.write(base64); res.close(); }); });
4:訪问地址
localhost:8080/http://baidu.com
时间: 2024-11-05 13:15:46