我正在搜索谷歌以获取可以捕获任何网站或URL图像的任何js库。我知道phantomjs库可以做到。在这里,我有一个小代码,可以捕获github主页并将其转换为png图像

如果有人熟悉phantomjs,那么请告诉我这行的含义是什么

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


在这里我可以给任何名字代替网页吗?

如果我需要捕获任何网页的一部分,那么该库如何帮助我。任何人都可以引导我。

var page = require('webpage').create();
page.open('http://github.com/', function () {
    page.render('github.png');
    phantom.exit();
});


https://github.com/ariya/phantomjs/wiki

谢谢

最佳答案

这是用于捕获图像的简单phantomjs脚本:

var page = require('webpage').create(),
system = require('system'),
address, output, size;


address = "http://google.com";
output = "your_image.png";
page.viewportSize = { width: 900, height: 600 };

page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.render(output);
            console.log('done');
            phantom.exit();
        }, 10000);
    }
})


哪里..

“地址”是您的网址字符串。

“输出”是您的文件名字符串。

同样,“宽度”和“高度”是网站要捕获的区域的尺寸(如果需要整个页面,请注明)

要从命令行运行此文件,请将上面的文件另存为'script_name.js,然后启动幻象,使js文件成为第一个参数。

希望这可以帮助 :)

09-25 18:03
查看更多