Ice Ice - 6 months ago 14
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:

http://www.tpex.org.tw/web/stock/aftertrading/daily_mktval/mkt.php?l=en-us

, I create somethink.

my $url = 'http://www.tpex.org.tw/web/stock/aftertrading/daily_mktval/mkt.php?l=en-us';


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


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

I'll be grateful for each help :)

Answer

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 = 'http://www.tpex.org.tw/web/stock/aftertrading/daily_mktval/mkt_print.php?l=en-us&d=2016/06/04&s=0,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";
}