in reply to How to obtain text in Mojo::DOM ?

Put your html in a single string. This way you know if you match things you should not match, like so:

#!/usr/bin/perl use Mojo::DOM; my $dom = Mojo::DOM->new(); $|=1; $text=' <a class="ab">2016-05-18</a> <a class="a b">2016-05-19</a> <a class="b">2016-05-20</a> <a class="a">2016-05-21</a> '; my $test1=$dom->parse($text)->at('a.ab')->text; print "18: $test1\n"; my @test2=$dom->parse($text)->find('a.a b')->map('text')->each; print "19: @test2 (bad)\n"; my $test3=$dom->parse($text)->at('a.a.b')->text; print "19: $test3\n";

18: 2016-05-18 19: 2016-05-18 2016-05-19 2016-05-20 2016-05-21 (bad) 19: 2016-05-19

Note, the find('a.a b') does not match anything, so to make it more interesting, I did a find('a. a b')

Replies are listed 'Best First'.
Re^2: How to obtain text in Mojo::DOM ?
by Perl_Love (Acolyte) on May 22, 2016 at 12:04 UTC
    use Mojo::DOM; $|=1; my $text='<span class="f-f60 fb">one</span>two</li> <li>three<span class="f-f60 fb" id="deliveryCount">WORK</span>five</li +> <li>ABC<span class="f-f60 fb" id="contactCount">XYZ</span> '; my $dom = Mojo::DOM->new($text); for my $e ($dom->find('span[id]')->each) { my $id=$e->{id}; if($id eq 'deliveryCount'){ print $id, ':', $e->text, "\n"; } }
    Thank you ! May I ask another question ? How to directly locate deliveryCount,get text content WORK? Is it not need to use loop method,such as for,to lookup spanid?
      my $foo=$dom->at('#deliveryCount')->text;
      I get it~