in reply to HTML::TreeBuilder:: identifing xpath-expression - first attempt

XPath is a W3C Recommendation. The documentation from that link will help you understand what XPather is outputting.

I looked at the source for the link (http://www.educa.ch/dyn/79376.asp?id=1187) you provided. All of the data that you are collecting appears to be in <div class="leerzeile"> elements.

It's generally better to target something like that than use hard-coded indices (e.g. div[3], div[4], etc.): if the site owner's decide to add some additional comment at the top of the page, what's in the 3rd <div> today may be in the 4th tomorrow; addresses won't necessarily have the same number of lines so perhaps you only want up to the 12th <div> or could need the 14th <div> as well.

Following the XPath link above, you'll see a large number of examples under Location Paths. One in particular stands out as close to what you want:

child::para[attribute::type="warning"] selects all para children of the context node that have a type attribute with value warning

So, you can probably get all the data you need by accessing div[attribute:class="leerzeile"] instead of a long list of div[N] paths. I didn't study the markup in minute detail: if that doesn't get everything you want, I'd still use that type of technique rather than opting for a hard-coded index.

Finally, just a note on the markup in your question: links are created with [url] and named links with [url|name] - this and related information are explained in Writeup Formatting Tips.

-- Ken

Replies are listed 'Best First'.
Re^2: HTML::TreeBuilder:: identifing xpath-expression - first attempt
by viveksnv (Sexton) on Oct 17, 2010 at 08:36 UTC
    Hi,

    Try with WWW::Mechanize

    It can solve your problem easily

    Regards,
    Vivek
      Hi viveksnv
      • kcott doesn't have the problem, Perlbeginner1 does (clicking the correct reply/comment on link is important)
      • WWW::Mechanize won't help him extract html by xpath queries
        I experimented with this:
        #!/usr/bin/perl use strict; use warnings; use HTML::TreeBuilder::XPath; use LWP::Simple; use Test::WWW::Mechanize; use WWW::Mechanize::TreeBuilder; use Data::Dumper::Concise; my $tree = HTML::TreeBuilder::XPath->new; my $mech = Test::WWW::Mechanize->new; WWW::Mechanize::TreeBuilder->meta->apply($mech); $tree->parse_content(get 'http://www.educa.ch/dyn/79376.asp?id=1187'); print Dumper($tree->as_text('//tr'));
        Update:: not finished - will integrate $mech->find_xpath later.
Re^2: HTML::TreeBuilder:: identifing xpath-expression - first attempt
by Anonymous Monk on Oct 17, 2010 at 09:18 UTC