Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^18: Need help with WWW::Mechanize and Chrome cookies

by Corion (Patriarch)
on Jul 19, 2021 at 07:44 UTC ( [id://11135161]=note: print w/replies, xml ) Need Help??


in reply to Re^17: Need help with WWW::Mechanize and Chrome cookies
in thread Need help with WWW::Mechanize and Chrome cookies

Your problem statement seems to continously change. I don't see/understand how you can get from "image download using its URL" to "image displayed using Javascript" without changing the problem entirely.

Maybe you can come up with a small, self-contained program that reproduces your problem, and also shows the response you receive and also tell us what parts you have already investigated, instead of letting us guess. That way, we can more easily reproduce what you see and maybe suggest better approaches.

If an image gets created using Javascript, you will have to fetch the image data from the browser memory, most likely by using some Javascript to fetch that image data. You can run arbitrary Javascript on a page using ->evaluate_in_page.

Replies are listed 'Best First'.
Re^19: Need help with WWW::Mechanize and Chrome cookies
by bakiperl (Beadle) on Jul 19, 2021 at 14:09 UTC
    Corion, forget the JavaScript for now. The goal is to use a single method to download all type of files (images, pdfs, CSVs etc..)
    According to the documentations, the following method should be able to do it.
    my @links = $mech->find_all_links(url_regex => qr/\.pdf/i); my @urls = map { $_->url_abs } @links; foreach (my $foo @urls ) { my $abs_path = "C:/path"; $mech->set_download_directory( $abs_path ); $mech->get($foo); }
    Unfortunately this method works only with files that the browser normally does not load such as (.csv). If you use this method with images (.jpg etc...) or PDFs (.pdf) the documents will load in the browser and the download fails. This exact same task has worked with WWW::Mechanize using the following method
    my $filename = "C:/path/filename"; my $foo = "http link"; $mech->get($foo, ':content_file'=>$filename);
    The workaround that you have suggested below didn't work with PDFs and CSVs. it worked with images only.
    my $response = $mech->get($url); my $c = $mech->content; # Dummy request to initialize everything open my $output, '>:raw', '/tmp/output.jpg'; print { $output } $response->decoded_content;

      The following gets at the content of images displayed on a page:

      #!perl use strict; use warnings; use 5.012; use WWW::Mechanize::Chrome; use Log::Log4perl ':easy'; Log::Log4perl->easy_init($TRACE); use File::Temp 'tempdir'; use Cwd; my $tempdir = tempdir(); my $mech = WWW::Mechanize::Chrome->new( headless => 1, data_directory => $tempdir, download_directory => cwd(), ); use Data::Dumper; my $res = $mech->get('https://egp.rutgers.edu/cgi/wmc.pl'); say Dumper $mech->getResourceTree_future()->get; my $link = $mech->xpath( '//a[text()="MY IMAGE"]', single => 1 ); $mech->click($link); $mech->sleep(1); my $resources = $mech->getResourceTree_future()->get; my @images = grep { $_->{type} eq 'Image' } @{$resources->{resources}} +; my $image = $mech->getResourceContent_future( $images[0]->{url} )->get +->{content}; open my $fh, '>:raw', 'test.jpg'; print $fh $image;

      Note that you will need a way to find which image is the one you want.

        Corion,
        Many thanks. the code has worked for a single image download. How about if you loop over multiple images like this:
        my @ids = qw(101 102 103 104 105); foreach my $id ( @ids ) { my $link = $mech->xpath( "//a[text()='MY IMAGE $id']", single => 1 + ); $mech->click($link); $mech->sleep(1); my $resources = $mech->getResourceTree_future()->get; my @images = grep { $_->{type} eq 'Image' } @{$resources->{resourc +es}}; print @images, "\n"; # this shows that the information in the arr +ay is not resetting my $image = $mech->getResourceContent_future( $images[0]->{url} )- +>get->{content}; open my $fh, '>:raw', $id.'.jpg'; print $fh $image; }
        This code does not necessarily save the correct images because it looks like the resources are not resetting. Thank you again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-07-22 10:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.