gpt4 book ai didi

javascript - 如何从 shell 中截取网页的一部分?

转载 作者:搜寻专家 更新时间:2023-11-01 04:19:38 24 4
gpt4 key购买 nike

我有一个网页的一部分,我需要在给定的时间间隔拍摄 gif 快照。快照需要全页大小分辨率,但正如我所说,它只会到达页面上的特定位置(在本例中它位于表格之后)。

像这样抓取页面快照图像的最佳方法是什么?我只想将它放入 cron 作业中,然后忘记它,但我还没有找到可以快速完成这项工作的工具。

解决方案:

按照@Eduardo 的出色指导,我实现了一个基于 phantomjs 和 imagemagick 的干净快速的解决方案(Mac:brew install phantomjs & brew install imagemagick):

*注意:如果您想完全删除 imagemagick,只需将以下内容添加到 rasterize.js:page.clipRect = { top: 10, left: 10, width: 500, height: 500 }

#! /usr/bin/env bash
# Used with PhantomJS - rasterize.js source: http://j.mp/xC7u1Z

refresh_seconds=30

while true; do
date_now=`date +"%Y-%m-%d %H%M"`

phantomjs rasterize.js $1 "${date_now}-original.png" # just sucking in the first arg from shell for the URL
convert "${date_now}-original.png" -crop 500x610+8+16 "${date_now}.png" # crop args: WIDTHxHEIGHT+LEFT_MARGIN+TOP_MARGIN
rm "${date_now}-original.png"

echo "Got image: ${date_now}.png - Now waiting ${refresh_seconds} seconds for next image..."
sleep ${refresh_seconds}
done

这里是上面phantomjs使用的js:

// As explained here: http://code.google.com/p/phantomjs/wiki/QuickStart

var page = new WebPage(),
address, output, size;

if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: rasterize.js URL filename');
phantom.exit();
} else {
address = phantom.args[0];
output = phantom.args[1];
page.viewportSize = { width: 600, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}

最佳答案

这里已经回答了这个问题: How can I take a screenshot/image of a website using Python?

它在 09 年得到了回答,但该选项仍然非常有效。我将尝试使用更多选项进行扩展。

这些工具将为您获取整页快照,稍后您可以使用 imagemagick 轻松地对其进行剪辑。

最近您可能有的另一个选择是 Phantomjs . Phantom 是一个在节点上运行的 headless 浏览器,它允许您拍摄整个页面或页面的一部分。

看看this example :

var page = new WebPage(),
address, output, size;

if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: rasterize.js URL filename');
phantom.exit();
} else {
address = phantom.args[0];
output = phantom.args[1];
page.viewportSize = { width: 600, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}

关于javascript - 如何从 shell 中截取网页的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com