如何使用 Perl 截取网页的屏幕截图? [英] How can I take screenshots of webpages with Perl?

查看:79
本文介绍了如何使用 Perl 截取网页的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用 Perl 编写一个脚本来打开不同的 URL 并保存每个 URL 的屏幕截图?

Is it possible to write a script in Perl that opens different URLs and saves a screenshot of each of them?

推荐答案

你可以使用 WWW::Mechanize::Firefox 控制 Firefox 实例并使用 $mech->content_as_png 转储呈现的页面.

You could use WWW::Mechanize::Firefox to control a Firefox instance and dump the rendered page with $mech->content_as_png.

请注意,设置它可能会带来相当大的挑战.

Be aware that setting it up can pose quite a challenge, though.

如果一切正常,您可以简单地使用这样的脚本来转储所需网站的图像,但您应该启动 Firefox 并手动将其调整为所需的宽度(高度无关紧要,WWW::Mechanize::Firefox 总是转储整个页面).

If all works as expected, you can simply use a script like this to dump images of the desired websites, but you should start Firefox and resize it to the desired width manually (height doesn't matter, WWW::Mechanize::Firefox always dumps the whole page).

use WWW::Mechanize::Firefox;
use Path::Class qw/file/;

my $mech = WWW::Mechanize::Firefox->new(
  bufsize => 10_000_000, # PNGs might become huge
);
$mech->get('http://www.stackoverflow.com/');

my $fh = file( 'test.png' )->open( '> :raw' );
print $fh $mech->content_as_png();

这篇关于如何使用 Perl 截取网页的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆