UPDATE: Seems that the data I was testing for landed right on a buffer chunk boundary. Not good. Let that be a lesson to all us part timers - boundaries suck!
====================================================
Recently, I had to drag my old perl knowledge up from the bottom of my brain to do a little db creation work.
I am taking a web page from our local chamber of commerce, where they give an alphabetical listing of their members, and sucking it in with Net::HTTP. I then cycle through the $buffer looking for occurances of "ID=XXXX". Those are links to "more info" on each company. Using the built-up array of IDs, I then pull each companies individual data.
Anyhow, to make a long story short (too late, right?) one business, serial number 3975, is always skipped!!! the regex
$buf =~ /ID=([0-9]+)/ seems to think 3975 doesn't match! The only answer I have found is to first write the main web page to a file, then read in the file. THEN it matches.
Here is the code I am/was using:
#!/usr/bin/perl
use strict;
use Net::HTTP;
use HTML::Strip;
use LWP::Simple;
my $DOMAIN="www.fentonchamber.org";
my $MAIN_LIST="AlphabeticalListing.asp";
my $HOMEDIR="/home/jrobiso2/Documents/CDS/Chamber/";
my $list_file="/tmp/AlphabeticalListing.html";
my @listing;
### Get initial listing of data from the main page.
open(SRC, "+>$list_file") or die "Cannot open file: $!\n";
my $http = Net::HTTP->new(Host => $DOMAIN) || die $!;
$http->keep_alive;
$http->write_request(GET => "/$MAIN_LIST", 'User Agent' => "Mozilla/5.
+0");
my($code, $mess, %h) = $http->read_response_headers;
## Build the listing of company numbers from the javascript window.ope
+n
## calls inside the main listing html page.
while (1) {
my $buf;
my $n = $http->read_entity_body($buf, 1024);
die "read failed: $!" unless defined $n;
last unless $n;
# if ($buf =~ /ID=([0-9]+)/) { # OLD CODE
# push @listing, $1; # THAT FAILED
# } #
print SRC $buf;
}
close(SRC);
open(SRC, "<$list_file") || die "Cannot open file: $!\n";
while (<SRC>) {
if (/ID=([0-9]+)/) {
push @listing, $1;
}
}
@listing = sort @listing;
From the code above you can see the actual site and page I am trying to steal from. If anyone can enlighten me as to what is wrong (what I have done wrong) I would greatly appreciate it, as this has taken 3 hours of my time and made me feel very stupid.
I am using perl 5.8.6.
What does this little button do . .<Click>;
"USER HAS SIGNED OFF FOR THE DAY"
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.