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内容
var page = require('webpage').create(); var server = require('webserver').create(); var sys = require('system'); var data = 'name=hacker&[email protected]'; if(sys.args.length !== 2){ console.log('Usage: app.js <portnumber>'); phantom.exit(1); } 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,{ keepAlive: true }, function (s) { 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
phantomjs环境搭建已经执行
时间: 2024-10-02 19:15:30