Ice Ice - 2 years ago 73
Perl Question

Getting data from table ?

how to display data (Stock name, Capitals, Close Price, Market value)from the website in terminal? I have this website:

, I create somethink.

my $url = '';

use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;

But I don't really know how to use
to print the data which I need.

I'll be grateful for each help :)

Answer Source

Simple pattern matching and some trick enough for to do it.

In your task $content contain the whole text.

First, extract the content of the body from the $content by using .+ with s flag. s flag helps to allow, match the any character with new line.

Second, split the extracted data by using </tr>.

Third, Iterate the foreach for the array then again will do patter matching with grouping for extract the data.

Here $l1 and $l2 stores the rank and stock code. And the other data are stored into the @arc variable

my $url = ',asc,0';
use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;

my ($table_body) = $content =~m/<tbody>(.+)<\/tbody>/s;

my @ar = split("</tr>",$table_body);

foreach my $lines(@ar)
    my ($l1,$l2,@arc) = $lines =~m/>(.+?)<\/td>/g;
    $, = "\t\t";
    print @arc,"\n";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download