Search code examples
perlwww-mechanize

how to get the query result from this website?


I want to get the stock data from the website (http://www.tdcc.com.tw/smWeb/QryStock.jsp) via perl WWW::Mechanize. For example, get the data by the stock number: 2330. The code below seems to POST data OK, but not get the response data like the Manual operation does.

use WWW::Mechanize;

my $stockno= '2330';
my $scadate= '20160422'; 
my $mech=WWW::Mechanize->new();

my $url='http://www.tdcc.com.tw/smWeb/QryStock.jsp';
$mech->get($url);
$mech->success or die $mech->response->status_line;
$mech->form_number(1);
$mech->field('SCA_DATE' => $scadate);
$mech->field('SqlMethod' => 'StockNo');
$mech->field('StockNo' => $stockno);
$mech->field('StockName'    => '');
$mech->field('sub'    => '查詢');
$mech->click_button('name' => 'sub');
$mech->success or die "post failed: ", $mech->response->status_line;
print $mech->response->status_line, "\n";
print $mech->response->content();

Solution

  • The problem is most likely that the site uses JavaScript, which isn't supported by WWW::Mechanize

    You should take a look at WWW::Mechanize::Firefox, which uses a running copy of Firefox to provide a similar API to the basic WWW::Mechanize. You will need to install the Mozrepl plugin for Firefox for it to work properly