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

HTML::SimpleParse to parse HTML

by techie411 (Acolyte)
on Dec 10, 2012 at 21:48 UTC ( #1008171=perlquestion: print w/ replies, xml ) Need Help??
techie411 has asked for the wisdom of the Perl Monks concerning the following question:

I am using the module HTML::SimpleParse to attempt to parse HTML and I'm not really understanding how to parse. Can someone give me a quick example to read the HTML to get what I want. I'm trying to grab the 'href' from all the lines that have "Completed". Example of HTML below. Thanks in advance!
<div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id01">Completed 3:46 pm, 06 Dec, 2012< +/a></span> </div> <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id02">Completed 4:00 pm, 06 Dec, 2012< +/a></span> </div>

Comment on HTML::SimpleParse to parse HTML
Download Code
Re: HTML::SimpleParse to parse HTML
by Anonymous Monk on Dec 10, 2012 at 23:56 UTC
Re: HTML::SimpleParse to parse HTML
by ww (Bishop) on Dec 11, 2012 at 01:01 UTC
    techie411: perhaps you should put a little tech in your life: perlre, perlretut (and many more perldocs) or read about regexen here, in Tutorials.

    Snippy? Not really. We like to see that those who pose problems have done a bit of work on their own to solve them. (We also like code that show what you've tried and error messages (if they exist) and an explanation of how your code falls short of your desires.

Re: HTML::SimpleParse to parse HTML
by bitingduck (Friar) on Dec 11, 2012 at 05:32 UTC

    Try running this:

    #!usr/bin/perl #simpleparse.pl use strict; use warnings; use HTML::SimpleParse; use Data::Dumper; my $html = <<'END'; <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id01">Completed 3:46 pm, 06 Dec, 2012< +/a></span> </div> <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id02">Completed 4:00 pm, 06 Dec, 2012< +/a></span> </div> END my $p=HTML::SimpleParse->new($html); print Dumper($p);

    And see what the data structure from HTML::SimpleParse looks like. It flattens everything out so that it removes all the structure that helps you figure out what you want. You could probably cook up some combination of conditionals to sort out what shows up in what order to get the urls you want, but a fancier module will make your life easier, at the expense of having to read more of the manual.

    Here's a solution using HTML::TreeBuilder (which uses HTML::Element for a lot of the interesting stuff)
    #!usr/bin/perl #simpleparse.pl use strict; use warnings; use HTML::TreeBuilder; use Data::Dumper; my $html = <<'END'; <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id01">Completed 3:46 pm, 06 Dec, 2012< +/a></span> </div> <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id02">Completed 4:00 pm, 06 Dec, 2012< +/a></span> </div> <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id03">Not Completed 3:46 pm, 06 Dec, 2 +012</a></span> </div> <div class="views-field views-field-title"> <span class="fiel +d-content"><a href="/MAKER/gen/id04">Something done 4:00 pm, 06 Dec, +2012</a></span> </div> END my $p=HTML::TreeBuilder->new_from_content($html); my @urls=$p->find('_tag'=>'a'); foreach (@urls){ if ($_->as_text=~/^Completed/){ print $_->attr('href')."\n"; } } #print Dumper($p);

    If you want to see how HTML::TreeBuilder parses your HTML into a structure, uncomment the last line with print Dumper($p). Compare that to what HTML::Simple does when it parses. It's a lot more complicated, but TreeBuilder and Element have a lot of methods to make that all mostly transparent.

      @bitingduck - Thanks a lot for providing an example, I appreciate it! I did end up using HTML::TreeBuilder! :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-08-22 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (153 votes), past polls