I haven't read your code closely, but I get the feeling that you're trying to use HTML::Parser to extract text below tags, especially the text contained in <a> tags together with the href attribute. HTML::Parser is a rather unwieldly tool to extract text below tags in my opinion - I prefer to use XPath expressions for such tasks. Depending on what tools you have available, you might want to use XML::LibXML or Web::Scraper or HTML::TreeBuilder::XPath (which is what Web::Scraper uses) for running the XPath expressions. I write the link+text extraction using Web::Scraper:
use strict; use Web::Scraper; use Data::Dumper; my $html = join "", <DATA>; # Invoked for a <a> tag my $link = scraper { process '//a' => 'href' => '@href'; process '//a' => 'description' => 'TEXT'; }; my $page = scraper { process '//a[@href]' => 'links[]' => $link; process '//meta[@http-equiv]' => 'meta[]' => '@content'; process '//area[@href]' => 'areas[]' => '@href'; }; my $info = $page->scrape($html); print Dumper $info; __DATA__ __DATA__ ... your html ...
Update: After some quick browsing of the CPAN for XPath, I found XML::XPathEngine, by mirod, and quite unsurprisingly XML::Twig already understands XPath expressions. So it should be quite feasible to jury-rig Web::Scraper to use XML::Twig instead of HTML::TreeBuilder as the underlying parsing engine. Or you might just use XML::Twig directly.
In reply to Re: HTML::Parser fun
by Corion
in thread HTML::Parser fun
by FreakyGreenLeaky
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |