Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: Nested div tag

by perlmad (Sexton)
on Jul 21, 2016 at 06:18 UTC ( [id://1168201]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Nested div tag
in thread Nested div tag

when i run the below code

#! usr/bin/perl use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new; $tree->parse_file(<<"HTML"); <div class="Row May-2015"> <div class="Cell month-year even"> May-2015 </div> <div class="Cell Release Date even"> 2015-06-25 </div> <div class="Cell National Mortgage Contract Rate even" +> 3.750 </div> </div> HTML my @rows = $tree->findnodes(q{//div[@class= 'Row May-2015']}); #my @rows=$tree->findnodes(q{ //div[ @class =~ /Cell/ ] }); print $#rows; for my $row ( @rows ){ my @cells = $row->findnodes(q{ //div[ @class =~ /Cell/ ] }); for my $cell ( @cells ){ #Dance( $cell ); print $cell; } }

output be like

[ab@linux0059 html_tree_builder]$ perl code.pl -1[ab@linux0059 html_tree_builder]$

It does not take div node

Replies are listed 'Best First'.
Re^5: Nested div tag
by Anonymous Monk on Jul 21, 2016 at 07:07 UTC

    That is some strange filename

    #!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new_from_content(<<'HTML'); <body> <div class="Row May-2015"> <div class="Cell month-year even"> May-2015 </div> <div class="Cell Release Date even"> 2015-06-25 </div> <div class="Cell National Mortgage Contract Rate even" +> 3.750 </div> </div> HTML $tree->dump; my @rows = $tree->findnodes(q{//div[@class =~ /Row/ ]}); print 0+@rows, "\n"; for my $row ( @rows ){ my @cells = $row->findnodes(q{ //div[ @class =~ /Cell/ ] }); print 0+@cells, "\n"; for my $cell ( @cells ){ print $cell->as_trimmed_text, "\n"; } } __END__ <html> @0 (IMPLICIT) <head> @0.0 (IMPLICIT) <body> @0.1 <div class="Row May-2015"> @0.1.0 <div class="Cell month-year even"> @0.1.0.0 " May-2015 " <div class="Cell Release Date even"> @0.1.0.1 " 2015-06-25 " <div class="Cell National Mortgage Contract Rate even"> @0.1.0.2 " 3.750 " 1 3 May-2015 2015-06-25 3.750

      I think this is not a exact output what i am expecting.

      #!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder::XPath; my $month; my $date; my $value; my $tree= HTML::TreeBuilder::XPath->new_from_content(<<'HTML'); <body> <div class="Row Jun-2015"> <div class="Cell month-year "> Jun-2015 </div> <div class="Cell Release Date "> 2015-07-30 </div> <div class="Cell National Mortgage Contract Rate "> 3.850 </div> </div> <div class="Row Jul-2015"> <div class="Cell month-year "> Jun-2015 </div> <div class="Cell Release Date "> 2015-08-31 </div> <div class="Cell National Mortgage Contract Rate "> 3.750 </div> </div> </body> HTML my @rows = $tree->findnodes(q{//div[@class =~ /Row Jun-2015/ ]}); print $#rows, "\n"; for my $row ( @rows ){ my @cells = $row->findnodes(q{ //div[ @class =~ /Cell/ ] }); print $#cells, "\n"; $date=$cells[1]->as_trimmed_text; $value=$cells[2]->as_trimmed_text; for my $cell ( @cells ){ print $cell->as_trimmed_text, "\t"; } print "Date: $date, value : $value\n"; }

      output be like

      0 5 Jun-2015 2015-07-30 3.850 Jun-2015 2015-08-31 3.750 + Date: 2015-07-30, value : 3.850

      But the expected output is :

      1 3 Jun-2015 2015-07-30 3.850 Date: 2015-07-30, value : 3.850 3 Jun-2015 2015-08-31 3.750 Date: 2015-08-31, value : 3.750

      there is something wrong with this code please give a suggestion to find out issue.

        You changed the @query again by adding a month-year to it ... beeing extra specific is fine if you're only interested in one single date ... but then why have a loop

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found