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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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();

In reply to Re^4: How to extract links from a webpage and store them in a mysql database by syedahmed.uos
in thread How to extract links from a webpage and store them in a mysql database by syedahmed.uos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found