Yeah, parsing HTML is a royal pain. All of the mixing of content and presentation. Ack. If only more people would use CSS to describe the markup semantically. For example, if the home node's had tables that looked like this:

<table class='adfu'> ... </table> ... <table class='body'> <table class='userdata'> ... </table> </table>

Then it would be dead easy to rip through the html and grab whatever section you want (not that I'm complaining -- I haven't even put in a suggestion or anythin'.) It would also make theming the site simpler as all you'd need to do is pass around a couple of style sheets.

Anyhow, I think I'm drifting Off Topic.

Given this problem, I would use LWP::Simple and HTML::Parser (which I find more generally useful than HTML::TreeBuilder, although you might also want to check out HTML::SimpleParse) What we've got to do is examine the html source and discover the meaning embedded within the page's structure. (By the way, for this reason, I think it's almost always better to work with raw rather than interpreted html.)

In this case, getting the user data from the home node turns out to be pretty easy. All you need to do is grab the data out of the sixth table in the html. Here's a start:

use LWP::Simple; use HTML::Parser; my $monk = $ARGV[0] || 'eg'; my $content = get("http://perlmonks.org/?node=$monk"); my $tables_seen = 0; my $in_user_table = 0; my $parser = HTML::Parser->new( start_h => [ sub { if ( $_[0] eq 'table' ) { $in_user_table = 1 if ++$tables_seen = += 6; } }, "tagname" ], end_h => [ sub { $in_user_table = 0 if $_[0] eq 'table'; }, "tagname" ], text_h => [ sub { print @_ if $in_user_table; }, "dtext" ], ); $parser->parse( $content ); $parser->eof;

The anonymous subs in start_h, stop_h and text_h are event handlers that are called when the parser encounters a start-element tag, a end-element tag and text, respectively. The string after the anonymous subs specifies what sort of argument the handlers receive.

What needs to be done now is to change the start, stop and text handlers to put the user data into a hash (note that for every table row, the key is in the first <td> and the value is in the second).

HTH.


In reply to Re: lwp diary: day 1 by eg
in thread lwp diary: day 1 by mkmcconn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.