Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Regex +WWW::Mechanize

by jayto (Acolyte)
on May 25, 2012 at 13:02 UTC ( [id://972418]=perlquestion: print w/replies, xml ) Need Help??

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

I started learning Perl 2 days ago so please don't bash. I am trying to make a program that scrapes web links off a page using WWW::MECHANIZE. I am also trying to seperate the internal link (the ones that link to other parts of the site), from the externals links(The ones that link to other sites). Of course the code I made does not work correctly, the internal and external links are still mixed together after the script is run. Im %99 sure that my regex is incorrect. Here is my code:

use WWW::Mechanize; use Data::Dump qw(dump); &crawl; sub crawl{ my $browser = WWW::Mechanize->new; my $url = 'https://www.centos.org/modules/news/article.php?storyid +=384'; my $domain = 'centos.org'; my @all_links = getLinks($browser, $url); my @internal_links = getLinks($browser, $url, qr/($domain|!http\:) +/i); my @external_links = getLinks($browser, $url, qr/(http\:&!$domain/ +i); return 1; } sub getLinks{ my $browser = shift; $browser->get( shift ); my @links = (); my @current_page_links = $browser->find_all_links(url_regex => sh +ift); for(@current_page_links){ push(@links,"$_->[0]"); } return @links; }

Replies are listed 'Best First'.
Re: Regex +WWW::Mechanize
by Anonymous Monk on May 25, 2012 at 13:46 UTC

    I started learning Perl 2 days ago so please don't bash.

    csh csh csh csh csh

    Print links with titles, make links absolute, external links only

    scrape.pl http://perl.org a //a/@href --uri=2 | ack -v "\Qhttp://perl.org\E"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found