Search code examples
regexperlwww-mechanize

Parsing links with Perl regex


I'm parsing a website with WWW::Mechanize to download some images. I need to populate an array with all links related to the resolutions available of the given image. But I need to populate only with links that are equal or less than '1440x900', but I'm not sure where to make that validation. I was trying this:

my @resolutions = map {$_->url} grep {$_->url =~ /$_[0]-\d{4,4}x\d{3,4}/} $mech->find_all_links();

How can I make that validation to only get images equal or less than '1440x900'?

tnx in advance!

Edit:

I can't use find_all_images method of Mech because they end in .html. For instance, the links are like that:

http://www.wallpaper.com/view/some_image-2560x1600.html

just after following this links that you have the .jpg image displayed.


Solution

  • use 5.014;
    for my $link ($mech->find_all_links(url_abs_regex => qr/\d+x\d+\.html$/a)) {
        my ($w, $h) = $link->url =~ /(\d+)x(\d+)/a;
        if ($w <= 1440 && $h <= 900) {
            # do something
        }
    }