Finally, I (and zentara) finished scripting this project. Code I paste below. There is one problem: how I can add also some links to a NYSE.csv file? Links (Chart, Profile, More) of first ten lines must be added. Here, text (Chart, Profile and More) don't have a links, and for NYSE.csv file for first ten rows, these text (Chart, Pro, More) must have links (see http://finance.yahoo.com/actives?e=nq). Any help will move forward these project. Thanks.
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use HTML::TokeParser::Simple;
my $ua = LWP::UserAgent->new; # Define user agent type
$ua->agent('MyApp/0.1 ');
my @requests = ( 'http://finance.yahoo.com/actives?e=us', 'http://fina
+nce.yahoo.com/actives?e=o', 'http://finance.yahoo.com/actives?e=aq',
+'http://finance.yahoo.com/actives?e=nq', );
my @lin = "";
my $file = "trash";
open (FH, ">$file");
select(FH);
# loop thru them
foreach my $requested ( @requests ) {
print "STARTING $requested ###########################\n\n\n\n\n";
# Request object
my $req = GET $requested;
# Make the request
my $res = $ua->request($req);
my $con = $res->content;
# print "$con\n";
my $p = HTML::TokeParser::Simple->new( \$con );
while ( my $token = $p->get_token ) { # This prints all text in an HTM
+L doc (i.e., it strips the HTML)
next unless $token->is_text;
print $token->as_is, ";";
}
print "ENDING $requested ###########################\n\n\n\n\n\n"; } #
+ end of loop
close (FH);
select (STDOUT);
open (FG, "<$file") || die "Can't open $file for a reading: $!\n";
while (<FG>) {
push(@lin, $2) if $_ =~ /(Volume Leaders;US;NASDAQ;AMEX;NYSE;)(Sym +bo
+l;Name.*)/g;
}
close(FG);
foreach my $vf (@lin) { $vf =~ s/; ;/;/g; }
foreach my $vf (@lin) {
$vf =~ s/;/|/g;
}
foreach my $vf (@lin) {
$vf =~ s/Symbol\|Name\|Last Trade\|Change\|Volume\|Related Info\|/SYMB
+ +OL\|NAME\|LAST TRADE\|CHANGE\|VOLUME\|RELATED INFO\n/g;
$vf =~ s/Chart\|, \|Profile\|, \|More\|/Chart, Profile, More\n/g;
$vf =~ s/(\&)\|/\&/g; $vf =~ s/\| \(/ \(/g;
$vf =~ s/\|(\d|\d\d|\d\d\d)\.(\d|\d\d|\d\d\d)\|(\d|\d\d|\d\d\d)\:(\d|\
+ +d\d|\d\d\d)/\|$1\.$2 $3\:$4/g; }
my $date = localtime;
$date =~ s/ /_/g;
my $us = "US-$date.csv";
my $nasdaq = "NASDAQ-$date.csv";
my $amex = "AMEX-$date.csv";
my $nyse = "NYSE-$date.csv";
open (US, ">$us") || die "US.csv: $!\n";
open (NASDAQ, ">$nasdaq") || die "NASDAQ.csv: $!\n";
open (AMEX, ">$amex") || die "AMEX.csv: $!\n";
open (NYSE, ">$nyse") || die "NYSE.csv: $!\n";
print US $lin[1];
print NASDAQ $lin[2];
print AMEX $lin[3];
print NYSE $lin[4];
close (US); close (NASDAQ); close (AMEX); close (NYSE);
unlink $file; # delete a file 'trash'
exit 0;
-
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.