是否可以在没有图形界面的情况下对网页进行截图?
Is it possible to take a webpage's screenshot without a graphical interface?
我有兴趣使用 perl 以编程方式截取网页的屏幕截图。我来了across这个解决方案:
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.whosebug.com/');
my $fh = file( 'test.png' )->open( '> :raw' );
print $fh $mech->content_as_png();
但是当我尝试 运行 这段代码时,我得到:
Failed to connect to , problem connecting to "localhost", port 4242:
Connection refused at /usr/local/share/perl/5.20.2/MozRepl/Client.pm
line 144
有人建议此故障是由于端口关闭(Debian Jessie、Apache2、Perl 5.20)或 MozRepl 未安装或未正确配置所致。
Debian服务器只有命令行,除此之外没有图形界面。当我 运行 cpan WWW::Mechanize::Firefox
我最后得到:
CPAN: Storable loaded ok (v2.49_01)
Reading '/root/.cpan/Metadata'
Database was generated on Fri, 22 Jul 2016 17:53:33 GMT
CPAN: Module::CoreList loaded ok (v5.20150214)
WWW::Mechanize::Firefox is up to date (0.79).
当我 运行 cpan MozRepl
时,我得到了最后:
CPAN: Storable loaded ok (v2.49_01)
Reading '/root/.cpan/Metadata'
Database was generated on Fri, 22 Jul 2016 17:53:33 GMT
CPAN: Module::CoreList loaded ok (v5.20150214)
MozRepl is up to date (0.06).
所以如题所问,如果没有图形界面,上面的代码能行吗?如果是,我该如何调试和解决这个问题?
2 个解决方案:
Xvfb 创建无头 X 服务器,并在其上安装 firefox 实例 运行
使用 Phantom,这似乎是一个很好的教程:http://blogs.perl.org/users/robhammond/2013/02/web-scraping-with-perl-phantomjs.html
我有兴趣使用 perl 以编程方式截取网页的屏幕截图。我来了across这个解决方案:
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.whosebug.com/');
my $fh = file( 'test.png' )->open( '> :raw' );
print $fh $mech->content_as_png();
但是当我尝试 运行 这段代码时,我得到:
Failed to connect to , problem connecting to "localhost", port 4242: Connection refused at /usr/local/share/perl/5.20.2/MozRepl/Client.pm line 144
有人建议此故障是由于端口关闭(Debian Jessie、Apache2、Perl 5.20)或 MozRepl 未安装或未正确配置所致。
Debian服务器只有命令行,除此之外没有图形界面。当我 运行 cpan WWW::Mechanize::Firefox
我最后得到:
CPAN: Storable loaded ok (v2.49_01)
Reading '/root/.cpan/Metadata'
Database was generated on Fri, 22 Jul 2016 17:53:33 GMT
CPAN: Module::CoreList loaded ok (v5.20150214)
WWW::Mechanize::Firefox is up to date (0.79).
当我 运行 cpan MozRepl
时,我得到了最后:
CPAN: Storable loaded ok (v2.49_01)
Reading '/root/.cpan/Metadata'
Database was generated on Fri, 22 Jul 2016 17:53:33 GMT
CPAN: Module::CoreList loaded ok (v5.20150214)
MozRepl is up to date (0.06).
所以如题所问,如果没有图形界面,上面的代码能行吗?如果是,我该如何调试和解决这个问题?
2 个解决方案:
Xvfb 创建无头 X 服务器,并在其上安装 firefox 实例 运行
使用 Phantom,这似乎是一个很好的教程:http://blogs.perl.org/users/robhammond/2013/02/web-scraping-with-perl-phantomjs.html