#!perl! -w use strict; use Data::Dumper; use Win32::IE::Mechanize; use HTML::TreeBuilder; #I/O open (LOG, '>', 'NJ\NJ_get_log.txt') || die "Unable to write to log file\n"; select LOG; $| = 1; select STDOUT; #Make perl unbuffer log stream sub new_bot { my $newbot = Win32::IE::Mechanize->new( { visible => 1 } ); return $newbot; } sub pass_disclaimer { my $bot = shift; print LOG "On disclaimer page, accepting.\n"; print "On disclaimer page, accepting.\n"; $bot->form_number(2); my $submit = ($bot->current_form->inputs())[0]; $submit->click(); sleep 2; $bot->get( 'https://www6.state.nj.us/LPS_spoff/geographicsearch.jsp' ); sleep 5; until ( $bot->success ) { print "Waiting for page to load...\n"; sleep 5; } if ( @_ ) { my $county = shift; $bot = search( $county, $bot ); } return $bot; } sub search { ## Begins search on passed county, returning the bot on the first search page my $county = shift; my $bot = shift; # Load Search print "Loading search page.\n"; eval { # Search Page 1 $bot->get( 'https://www6.state.nj.us/LPS_spoff/geographicsearch.jsp' ) unless $bot->uri eq 'https://www6.state.nj.us/LPS_spoff/geographicsearch.jsp'; # $bot->get( 'https://www6.state.nj.us/LPS_spoff/geographicsearch.jsp' ); sleep 5; until ( $bot->success ) { print "Waiting for page to load...\n"; sleep 5; } $bot = pass_disclaimer($bot) if $bot->uri eq 'http://www.nj.gov/njsp/info/reg_sexoffend.html'; $bot->form_name( 'gs' ); $bot->select( 'county' => "$county" ); sleep 1; for ( $bot->current_form->inputs() ) { $_->click() if $_->name() eq 'subC'; } sleep 4; # Search Page 2 # $bot->form_name( 'gsm' ); my @forms = $bot->forms(); print "Search page2 forms: @forms\n"; print "Number of page2 forms = ".@forms." = 3?\n"; # for ( @forms ) { # print "Form name: ",$_->name(),"\n"; # my @inputs = $_->inputs(); # print "Form inputs: @inputs\n"; # print "# of inputs = ".@inputs."\n"; # for my $in (@inputs) { # print "Input name: ",$in->name(), "\n"; # print "Input type: ",$in->type(), "\n"; # } # } # $bot->form_number(1); # print "Selected 1st form\n"; # $bot->form_number(2); # print "Selected 2nd form\n"; # $bot->field( 'municipality' => ' 13 : 30 : ABERDEEN TWP : ABERDEEN TWP : MONMOUTH ' ); # $bot->submit(); # $bot->form_name( 'gsm' ); # print "Selected gsm form.\n"; # $bot->submit_form( form_number => 2 ); # print "Submitted form 2?\n"; # print "Form name = ",$bot->current_form->name(),"\n"; # my $submit = ($bot->current_form->inputs())[6]; # print "Submit type: ",$submit->type(),"\n"; # $submit->click(); # $bot->click(); # print "Clicked submit?\n"; }; if ( $@ ) { print LOG "$county: error in search: ",$@,".\n"; print "$county: error in search: ",$@,".\n"; $bot->close; return search( $county, &new_bot() ); } return $bot; } # Main { my $mech = new_bot; $mech = search( '13MONMOUTH', $mech ); }