I recently made a little script to come up with an index for the increasingly unwieldy
Name Space thread. This led me into an area I've not explored before (one of many, I hasten to add), that of extracting info from HTML. I wanted something that wd go through the page and pull out a node number and name for the first post by each monk who had contributed to the thread. What I came up with was this:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard :cgi-lib);
use LWP::Simple;
my $url ="http://perlmonks.org/index.pl?node_id=110166";
my $html = get $url or die "can't get url $!";
my %names;
#find the names and node ids:
while ($html =~ s/=(\d*?)&lastnode_id=110166">[^<]*<\/A><BR> by <A HRE
+F="\/index\.pl\?node_id=\d*&lastnode_id=110166">(.*?)<\/A> on \w{3} \
+d{2}, \d{4} at \d{2}:\d{2}//s) {
$names{$2} = $1 unless $names{$2};
}
# print out a page of links to nodes:
for (sort { lc($a) cmp lc($b) } keys %names) {
print "<A HREF=\"/index.pl?node_id=$names{$_}&lastnode_id=110166\"
+>$_</A> | ";
}
... which does the job, BUT the regex is big and fat and ugly, and I just wondered whether there was a more elegant, less impenetrable way to do it (i.e. I wondered how *many* such ways there were). I looked at HTML::Parser, but (and perhaps my inspection was too cursory) it didn't seem as though it wd help me much in pulling out bits of tags, as I need to here. Also, I felt that the
while loop was a bit clumsy... but couldn't see a quicker way to capture two matches into a hash. I'd be very interested in any suggestions how to do this better.
§
George Sherston
Edit: chipmunk 2001-11-11
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.