Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to obtain text in Mojo::DOM ?

by Perl_Love (Acolyte)
on May 22, 2016 at 09:26 UTC ( [id://1163786]=perlquestion: print w/replies, xml ) Need Help??

Perl_Love has asked for the wisdom of the Perl Monks concerning the following question:

Hello Kindly help me to answer this question AB no spaces can be obtained by the text.If A and B there is space,we can not get text? Wish somebody can help to explain,thanks ~
use Mojo::DOM; my $dom = Mojo::DOM->new(); $|=1; my $test1=$dom->parse('<a class="ab">2016-05-18</a>')->at('a.ab')->tex +t; print "$test1\n"; my $test2=$dom->parse('<a class="a b">2016-05-18</a>')->at('a.a b')->t +ext; print "$test2\n";

Replies are listed 'Best First'.
Re: How to obtain text in Mojo::DOM ?
by haukex (Archbishop) on May 22, 2016 at 09:41 UTC

    Hi Perl_Love,

    There are two separate things going on here:

    First, class="a b" specifies that the anchor has two separate classes, named "a" and "b".

    Second, the selector 'a.a b' means to select an element b (bold) that is a descendant of an a element (anchor) of class "a".

    So to make your second example work, you could specify the selector to match an anchor that has both classes by chaining them, giving "a.a.b":

    use Mojo::DOM; my $dom = Mojo::DOM->new(); my $test2=$dom->parse('<a class="a b">2016-05-18</a>')->at('a.a.b')->t +ext; print "$test2\n"; __END__ 2016-05-18

    Hope this helps,
    -- Hauke D

      great~thank a lot! it's work! haha~
Re: How to obtain text in Mojo::DOM ?
by LanX (Saint) on May 22, 2016 at 09:43 UTC
    Spaces are not allowed in html identifiers.

    ->at('a.a b') can't work, since a and b are different classes.

    Probably ->at('a.a')->at('a.b') does, but that's only a guess without reading the documentation of Mojo::DOM .

    update

    After reading the docs, give this a try

    ->at('a[class~="a"][class~="b"]');

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: How to obtain text in Mojo::DOM ?
by FreeBeerReekingMonk (Deacon) on May 22, 2016 at 09:47 UTC
    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')

      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~

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found