*Brittle* but works right now… exercise for the reader to correlate headers with data or dig deeper or adjust scraping. I would really think that if you’re a TA, GA, or prof or whatever that the university would probably install a crontab for you that would be *much* more robust. Something like–
30 2 * * * `echo "…SQL statement and math…" | mysql students_db | mail you@your.edu -s "Your cron"`
–would be pretty trivial on the backend for someone I would think. Certainly easier and more likely to keep working than–
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;
use HTML::TableExtract;
# Firefox will be in a different place/name for different architecture
+s.
my $mech = WWW::Mechanize::Firefox->new(
activate => 1,
autoclose => 1,
launch => "/Applications/Firefox.app/Contents/MacOS/firefox" );
$mech->get("https://webapp4.asu.edu/catalog/Home.ext");
eval {
my ( $val,$type ) = $mech->eval_in_page(<<'JS');
jQuery(function($){
// Click the ASU campus+online radio button.
$("input[name='typeSelection'][value='C']").click();
});
JS
};
die $@ if $@;
# Get the desired search result page.
$mech->get("https://webapp4.asu.edu/catalog/course?s=MAT&n=243&c=DTPHX
+&t=2144&f=INTRT&r=44843");
my @headers = ( qr/ Reserved \s+ Available \s+ Seats /x,
qr / Students \s+ Enrolled /x,
qr/ Total \s+ Seats \s+ Reserved /x,
qr/ Reserved \s+ Until /x, );
my $te = HTML::TableExtract->new( headers => \@headers );
$te->parse($mech->content);
for my $row ( $te->rows )
{
no warnings "uninitialized";
s/\A\s+//g, s/\s+\z//g, s/\s/ /g for @$row;
next unless grep length, @$row;
print "Scraped info: ", join(',', @$row), "\n";
}
Scraped info: 10,40,50,n/a
Reading: WWW::Mechanize::Firefox, HTML::TableExtract, jQuery (note, they are using a positively ancient version right now: 1.2.3). |