Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

HTML::TreeBuilder - Multiple instances of text in the same tag

by Uggles (Novice)
on Jul 10, 2013 at 17:35 UTC ( #1043511=perlquestion: print w/replies, xml ) Need Help??
Uggles has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use TreeBuilder to get all of the strings in a certain tag of an HTML document into an array. I use find('mytag') which works fine to get all of the elements, but if I just print the array created from that then it prints HTML::Element hashes instead of the strings. That makes sense considering HTML::Element has the command "as_text". When I try to use "as_text" in the following way:

my @parsed = $element -> find('tag'); -> as_text(); print @parsed;

then it will only print the first instance of text within the desired tag instead of all of them. If I try and use "as_text" on @parsed in the following way:

my @parsed = $element -> find('b'); @parsed -> as_text(); print @parsed;

then I get a "Can't call method 'as_text' without package..." error. Is there a specific syntax for doing this that I just am not getting?

Replies are listed 'Best First'.
Re: HTML::TreeBuilder - Multiple instances of text in the same tag (perlintro)
by Anonymous Monk on Jul 10, 2013 at 23:48 UTC

    Is there a specific syntax for doing this that I just am not getting?

    Yes, read perlintro , esp arrays and foreach

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043511]
Approved by ww
help
Chatterbox?
[1nickt]: hippo lately I can tell just by the fact that Anonymous posts a reply after some hours that it is more than likely That Monk.
[virtualsue]: weird. my p6 simple web spider fails to extract anchor tags from perlmonks.org
[holli]: Nice. Turns out I don't have to remove Sweet home Alabama from my playlist ;)
[Corion]: virtualsue: We have some links with a newline between <a and the attributes, maybe your extractor fails to handle that?

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-12-13 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (360 votes). Check out past polls.

    Notices?