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

WWW::Mechanize follow_link()

by cajun (Chaplain)
on Sep 13, 2005 at 06:15 UTC ( [id://491486]=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to scrape a web page with links that lead to links that lead to more links.

# get the first level of links my @links = $mech->find_all_images(url_regex => qr/jpg$/); # follow each link to the next link foreach (@links) { $mech->follow_link ( $_->url() )};

When I run this, I get the following error:

Odd number of elements in hash assignment at /usr/lib/perl5/vendor_per +l/5.8.6/WWW/Mechanize.pm line 573.
I've done a super search and I've not yet been able to find any clues yet. I haven't a clue what I'm doing wrong. Thanks for any pointers or links.

Thanks,
Mike

Replies are listed 'Best First'.
Re: WWW::Mechanize follow_link()
by johnnywang (Priest) on Sep 13, 2005 at 06:32 UTC
    The method follow_link takes a hash:
    $mech->follow_link( text => "download", n => 3 )
    you are getting your error since you're passing one element: the url. In fact, you don't need to use follow_link, just use:
    $mech->get($_->url);
    If you're trying to download the images (looks like it from your regex), you may simply do:
    $mech->mirror($_->url_abs, "some_local_file_name");
Re: WWW::Mechanize follow_link()
by friedo (Prior) on Sep 13, 2005 at 06:34 UTC
    follow_link takes named parameters, not a single URL, but there's no need to use it if you know the URL already. Just use $mech->get( $_->url );
Re: WWW::Mechanize follow_link()
by cajun (Chaplain) on Sep 13, 2005 at 06:40 UTC
    Thanks johnnywang and friedo. That gives me some clues, but, these are not the last links in the chain. They are the second link in a chain of 3 or 4 links. But this definately helps.

    Thanks,
    Mike

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found