I have interest to programmatically take a screenshot of a webpage using perl. I have come across this solution:
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();
but when I try to run this code I get:
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
It was suggested that this failure is due to a closed port (Debian Jessie, Apache2, Perl 5.20), or MozRepl not installed or not configured correctly.
The Debian server is a command line only, and there is no graphic interface otherwise. When I run cpan WWW::Mechanize::Firefox
I get at the end:
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).
And when I run cpan MozRepl
, I get at the end:
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).
So as the title asks, can the above code work if there is no graphical interface? If yes, what can I do to debug and solve this issue?
2 solutions:
Xvfb to create a headless X server, and have the firefox instance running on it
Use Phantom, and this seems to be a good tutorial: http://blogs.perl.org/users/robhammond/2013/02/web-scraping-with-perl-phantomjs.html