hi,
i am new to perl and i have a problem. i saw ur code and thought if u could help me out.
i have to write a code to extract links from a webpage and store them in a mysql database.I have the script to extract the links and also i have stored them in the database but not as i want it to be stored. i did like to have each url in the "links" column to be stored in different rows. what i get is all values extracted from a given url are getting stored in one cell and are seperated by commas. I want them to be inserted into different rows.
the follwoing is the code
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL; $url =<> ;; # for instance
$ua = LWP::UserAgent->new; # Set up a callback that collect image l
+inks
my @a = (); #@imgs = (); whereever i find img i change to a for hype
+rlinks.
sub callback {
my($tag, %attr) = @_;
return if $tag ne 'a';
push(@a,values %attr);#push(@imgs, values %attr);
} # Make the parser. Unfortunately, we don't know the base yet (it
+ might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback); # Request document and parse
+ it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])}); # Expand all image URLs to absolut
+e 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=.....;host=mysql.serv......
+","xxxxxx","xxxxxx");
#$dbh->do("CREATE TABLE htmllinks (webpage VARCHAR(80) not null primar
+y key ,links VARCHAR(80) null)");
$dbh->do("INSERT INTO htmllinks VALUES ('$base',' @a')");
2006-12-05 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Re: proof of concept how to run this code'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.