Hi, I'm extracting info from a website using HTML::TreeBuilder::XPath. With the help of the monks I've been able to do this from other websites with almost no complication...until now. Basically, the foreach loop is not looping through the whole table in order to extract the info from each node. It's only retrieving the first results. I have tried this in many ways, but it's always the same result no matter the route I use for the node (even copying the whole XPath route from the browser by rightclicking on it). This is the code, if you execute it and look at the sourcecode, you'll see what I mean.

#!/usr/bin/perl -w use LWP::Simple; use HTML::TreeBuilder::XPath; use Data::Dumper; use strict; my $debug=1; my $base='http://www.msccrociere.it/it_it'; my $url='/Partenza-Crociere/Trova-La-Tua-Crociera.aspx?Reg=CAR&DateF=2 +01211&ddl=n&p=1&'; my $page = get($base.$url) or die $!; my $p = HTML::TreeBuilder::XPath->new_from_content( $page ); #binmode( STDOUT, ':utf8'); my @trips= $p->findnodes( '//table[@id="tblFYCXML_Itin"]'); foreach my $trip (@trips){ my $destination = $trip->findvalue('.//h2[@class="FYCm +aneDestXML"]'); my $shipname = $trip->findvalue('.//div[@class="cConte +ntLeft"]/a/h3'); print "$destination\n"; print "$shipname\n"; }

I know I'm making a newbie mistake somewhere, like I said I've tried many different things before asking here. I hope you can give me a hand. Thanks a lot!!


In reply to foreach loop not retrieving all data. by marcoss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.