Quick and dirty and not complete, but it works :)

#!/usr/bin/perl use LWP::Simple; use HTML::TokeParser::Simple; use Data::Dumper; use strict; use warnings; my $start = 'http://155.69.224.75:8000/eeepeople/AcadStaff.asp'; my $file = './index.html'; LWP::Simple::mirror($start, $file); my $p = HTML::TokeParser::Simple->new($file); my $state = 0; my ($url, $name, @teachers ); while (my $t = $p->get_token) { if ($state == 0) { if ($t->is_start_tag('a')) { my $attr = $t->return_attr; if (exists $attr->{href} and $attr->{href} =~ /\/cv\//) { $url = $attr->{href}; $state = 1; } } } elsif ($state == 1) { if ($t->is_end_tag('a')) { push @teachers, { name => $name, url => $url }; $name = ''; $url = ''; $state = 0; } elsif ($t->is_text) { $name .= $t->as_is; } } } print Dumper(\@teachers), "\n"; foreach my $teacher (@teachers) { my $filename = lc($teacher->{name}); $filename =~ s/\s+/_/g; $filename .= '.html'; LWP::Simple::mirror($teacher->{url}, $filename); my $p = HTML::TokeParser::Simple->new($filename); $state = 0; my ($pub, $res, @publications, @interests); while (my $t = $p->get_token) { if ($state == 0) { if ($t->is_text and $t->as_is =~ /publication/i) { $state = 2; } } elsif ($state == 2) { if ($t->is_start_tag('li')) { $state = 3; } elsif ($t->is_end_tag('ul')) { $state = 0; } } elsif ($state == 3) { if ($t->is_text) { $pub .= $t->as_is; } elsif ($t->is_end_tag('li')) { push @publications, $pub; $pub = ''; $state = 2; } } } print $teacher->{name} ." published:\n"; print Dumper(\@publications), "\n"; }

Ciao, Valerio


In reply to Re: Need help extracting data from web page by valdez
in thread Need help extracting data from web page by shu

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.