我正在尝试实现表面上有用且功能强大的url-to-image module。使用基本的示例脚本,我可以将尝试的任何网站(我需要的网站使用angular)渲染并保存为.png。
我需要登录页面之后的内容,但是我什至无法渲染登录页面:http://momentum-leaderboard.herokuapp.com/#/login。
关于如何使此模块适用于此URL的任何想法?还是其他工具呈现这样的页面图像的想法?
最佳答案
仅PhantomJS可以解决此问题。该脚本应该足以呈现此登录页面:rasterizeSimple.js
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length != 3) {
console.log('Usage: rasterize.js URL filename');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 600, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 2000);
}
});
}
经过登录屏幕是另一回事。由于PhantomJS是Web浏览器,因此只需以登录用户的身份打开登录页面,填写表单,按登录按钮,等待几秒钟,然后重新调用感兴趣的URL,就可以了。登录表单可以通过不同的方式完成,如果您愿意,可以注入jquery。没有测试第二个脚本,但是它应该可以帮助您。
page.open(address, function (status) {
if (page.injectJs('jquery-1.11.2.js')) { // Loads from the working dir i think
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
page.evaluate(function() {
$('input[name=username]').val('username');
$('input[name=password]').val('password123');
$('input[name=submit]').click();
});
window.setTimeout(function () {
page.open(address, function (status) {
if (status == 'success') {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 2000);
}
});
}, 2000);
}
}
});
关于javascript - 网址到图片会呈现我尝试的每个网址,但我需要的网址除外,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41624477/