Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, I am new to Perl and tryi

Status
Not open for further replies.

canguro

Programmer
Sep 15, 2002
57
US
Hello,

I am new to Perl and trying to learn it by doing some reading and by writing some simple programs, for example I am trying to access a website and extract data for a pet project (but not for commercial use).

I found the module TableExtract in CPAN and successfully installed it. I am trying to access the table values in this site:


I have built some preliminary code, but unfortunately I cannot complete the task to access the data values in the table that comes up in the webpage. The 4 columns I am hoping to access are Company, Symbol, EPS Estimate, EPS Actual. I want to print each row in the table, showing the values of the 4 columns in each row.

This is the code I have written so far:

#!/usr/bin/perl

use warnings;
use strict;
use LWP::Simple;
use HTML::TableExtract;

my ($te, $url);

$url="
$te = new HTML::TableExtract( headers =>
[qw(Company Symbol)] );

$te->parse($url);


Can someone please give me some guidance on how to proceed? Your help would be greatly appreciated!

Many thanks,

Joe
 
Try:

my $url="my $content=get $url;

my $te = new HTML::TableExtract( headers =>
[qw(Company Symbol)] );

$te->parse($content);


You forgot to 'GET' the page before parsing it :)

Then from the POD you should be able to do something like:

# Examine all matching tables
foreach my $ts ($te->table_states) {
print "Table (", join(',', $ts->coords), "):\n";
foreach my $row ($ts->rows) {
print join(',', @$row), "\n";
}
}

to get the actual data.

Barbie
Leader of Birmingham Perl Mongers
 
Barbie,

Thank you for your code suggestions. The data is now starting to show, although I am getting one piece (field) on each line. Is there a way to force all columns to print out on 1 line (row) in the following fashion?:

a1, a2, a3, a4
b1, b2, b3, b4
.
.
.

I am also wondering if I can somehow 'grab' (save) the literal "Earnings Announcements for: Friday, May 2, 2003" which displays as a title above the table, since the date is crucial.

Many, many thanks for your insights,

Joe
 
Try using printf statements instead of print, it works just like in C, you can specify width for each value which should give you the columns u are looking for although you may have to play around with it to get it looking the way you want :)

Good Luck
--Marty

--Computable or not Computable that is not the question
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
 
Marty,

Thank you for the idea. I did try printf instead of print, but the result is unchanged. Each piece of data comes out on a separate line. Any suggestions?

Thanks,

Joe
 
chomp your data to get rid of newline characters ...
 
PaulTEG,

Thank you for the reply. Can you indicate where the chomp needs to go?

Joe
 
chomp each atom that wraps to a newline before printing, which according to your post is all of them

HTH
 
PaulTEG,

Thanks for your replies and suggestions. I hate to be so dense, but I only started learning Perl a short while ago, and even the 2 books I have on Perl make no reference to 'atoms' in the index at the back of the books. Looking at the last part of the code (shown below), can you please tell me how you would phrase the appropriate chomps? Here is the code:

my $te = new HTML::TableExtract( headers =>
[qw(Company Symbol Estimate Actual)] );

$te->parse($content);

#Examine all matching tables
foreach my $ts ($te->table_states) {
print "Table (", join(',', $ts->coords), "):\n";
foreach my $row ($ts->rows) {
print join(',', @$row), "\n";
}
}

Your help is greatly appreciated.

Joe
 
If you consider each (row, col) as an atom - my fault.

@$row=chomp(@$row);
print join (@,', @$row), "\n";

I can't test it, and I'm not quite sure it'll work, because I'm not toolong on the Perl Path either, but its worth a shot

Paul
 
Paul,

Many, many thanks for your advice! I will test it out first chance I get tomorrow, since it's getting late (2+ A.M.) here on the West Coast. But you are right! Each idea is definitely worth a shot!

I'm real hyped-up about the good things I hear about Perl and how much power it has, and each step forward I make is very satisfying. Seems that all you Perl folk are full of great ideas and very helpful.

Joe
 
Paul,

I tried your ideas but somehow it's not working. I think I must have chomped all over the place until my code looked like Swiss cheese. I'm sure my problem is the lack of experience with tables coming from webpages.

If anyone out there has any ideas or suggestions to offer they would be greatly appreciated.

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top