I have to write a program that will repeatedly extract links to a depth level of 3. i.e., after extracting links the first time i store the links in the mysql database. then i fetch these links and iterate the function over each link to extract more links.. so i have to do three times.
I have stored and then fetched the links from the database
i aint able to run the function on each link and store them back to the database.
the code is shown previously.
#!/usr/bin/perl
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL; $url =<>;; # for instance
#my $depth = 0;
my @link =();
my $ua = LWP::UserAgent->new; # Set up a callback that collect links
my @a = ();
sub callback {
my($tag, %attr) = @_;
return if $tag ne 'a';
push(@a,values %attr);
} # Make the parser.Unfortunately, we don't know the base yet (it mig
+ht be
#diffent from $url)
my $p = HTML::LinkExtor->new(\&callback);
my $res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])}); # Expand all image URLs to absolute ones
my $base = $res->base;
@a = map { $_ = url($_, $base)->abs; } @a; # Print them out
print join("\n", @a), "\n";
use strict;
use DBI();
my $dbh = DBI->connect("DBI:mysql:database=gatxp;host="","","");
#$dbh->do("CREATE TABLE newlinks (md5 INTEGER(100) not null primary k
+ey, webpage VARCHAR(80) not null)");
$dbh->do("INSERT INTO newlinks VALUES ('MD5','0','$base','1')");
foreach $a(@a){
$dbh->do ("INSERT INTO newlinks VALUES ('','1','$a','0')");
}
my $sth = $dbh->prepare('SELECT * FROM newlinks')
or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute();
while( my $ref = $sth->fetchrow_hashref()){
my $link=$ref->{'webpage'};
foreach $link(@link){
my $usa = LWP::UserAgent->new;
$p = HTML::LinkExtor->new(\&callback);
my $res = $usa->request(HTTP::Request->new(GET => $link),
sub {$p->parse($_[0])});
$base = $res->base;
@link = map { $_ = url($_, $base)->abs; } @link; # Print them out
print "$$link\n";
$sth->finish();
$dbh->disconnect();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.