How can I take screenshots of webpages with Perl?

后端 未结 5 1940
梦谈多话
梦谈多话 2020-12-01 05:04

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

相关标签:
5条回答
  • 2020-12-01 05:09

    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.

    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();
    
    0 讨论(0)
  • 2020-12-01 05:21

    Use a third-party web service API like http://webshotspro.com/ (screenshots) or http://www.thumbalizr.com/ (thumbnails).

    0 讨论(0)
  • 2020-12-01 05:27

    Another approach, which doesn't require the use of a browser, is to use ImageMagick and HTML2PS to convert the image. Be warned however, this isn't trivial, and it's near impossible (last I tried) to get this working on Windows properly.

    Once ImageMagick is installed, the simplest approach is to just run a system call to the convert program that ImageMagick installs. If you want a less hackish approach, you can use the PerlMagick ImageMagick API.

    There is an excellent discussion on this approach you can find on PerlMonks.

    0 讨论(0)
  • 2020-12-01 05:30

    You could also use Win32::IE::Mechanize to render the web page using IE, and then Win32::Screenshot to capture the page. You'll probably have to do a bit of work to figure out where to take the screenshot, but that shouldn't be too incredibly hard.

    This will be a Windows platform only solution, of course, but may suffice.

    0 讨论(0)
  • 2020-12-01 05:35

    Use the WWW::Selenium module, for which you'll need to have a Selenium Remote Control session up and running.

    The capture_entire_page_screenshot() method should get you up and running.

    From WWW::Selenium on CPAN:

    $sel->capture_entire_page_screenshot($filename, $kwargs)

    Saves the entire contents of the current window canvas to a PNG file...


    A typical script:

    use strict;
    use warnings;
    use WWW::Selenium;
    
    my $sel = WWW::Selenium->new( host => "localhost", 
                                  port => 4444, 
                                  browser => "*iexplore", 
                                  browser_url => "http://www.google.com",
                                );
    
    $sel->start;
    $sel->open("http://www.google.com");
    $sel->capture_entire_page_screenshot("screenshot.png");
    $sel->close;
    
    0 讨论(0)
提交回复
热议问题