First, let me apology for being so scarce lately. I have been unable to visit PM lately, and feel a little guilty for returning with what is probably a silly question.
I am trying to use HTML::Parser to extract some very specific links from a page but I also need to know what the text is from each link.
So, if a link is Foo, I want to be able to know that "Foo" corresponds to the url is links to. This has got to be easy, but it is late and my brain is numb.
I am using HTML::Parse to get the tags. Here is some code:
#!/usr/bin/perl
use strict;
use lib "/home/jon/perl"; # where BrowserEmulator is
use HTML::Parser;
use BrowserEmulator; # this gets all the text from the page
my @SSNB;
# start of ParseLink
{
package ParseLink;
our @ISA = qw(HTML::Parser);
# called by parse
sub start
{
my ($this, $tag, $attr) = @_;
if ($tag eq "a")
{
$this->{links}{$attr->{href}} = 1;
}
}
sub get_links
{
my $this = shift;
return keys %{$this->{links}};
}
}
my $test_url = shift;
my $string = &BrowserEmulator::getFullSource($test_url);
my $p = ParseLink->new;
$p->parse($string);
for ($p->get_links)
{
print "LINK: $_\n";
}
I admit it, I am Paco.
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.