in reply to Extracting full links from HTML
If you're a tad familiar with XPath (or if you want to become familiar with it), you can try HTML::TreeBuilder::XPath, which adds XPath support to HTML::TreeBuilder.
The code looks like this:
use warnings; use strict; use HTML::TreeBuilder::XPath; my $str = q{<a href ='some\\where'><img src='apple.gif'></img></a> <a href ='some\\where'>not an img</img></a> <a href ='some\\where'><img src='apple.gif'></img> and tex +t</a> }; my $root = HTML::TreeBuilder->new_from_content($str); foreach my $tag ($root->findnodes( '//a[./img]')) { print "link: ", $tag->as_HTML; }
If you want to capture the links that have only an image in the content, you can replace the condition by //a[./img and string()=""] (that doesn't exactly garantee that there's nothing else in the link, but getting the query 100% right is left as an exercise for the reader).
Of course if all you're interested in is the value of the src attribute, you can get it directly:
foreach my $url ($root->findnodes( '//a/img/@src')) { print "link: ", $url->getValue, "\n"; }
Note that in that case the attribute are returned as HTML::TreeBuilder::XPath::Attribute objects, hence you need to use getValue to get the value. Hummm.... I wonder if that's in the docs, if not I'll add it.
|
|---|