CLI command to pull Stock Price and volume from Google
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
CLI command to pull Stock Price and volume from Google
I have an app that has been pulling stock data from yahoo. The part that pulls it is a
Pascal program that is run by cron and executes the following CLI command:
This was executed as an external process to the Pascal program and then Pascal Progam would then capture the data and insert it into a mysql table. Then the data could be viewed through a GUI App.
Unfortunately yahoo has shut down the ability to pull data with scripts. The last day it worked was 10/31/2017. I am looking at replacing the Yahoo command with a Google command. I can get a stock quote with a:
It grabs the first "ref_" id line and removes the markup. Then it finds the "vol_and_avg" data-snapfield and grabs the "val" information line and removes the markup and the average value. This is a quick hack. There is most likely a better way to do this.
Did you try just having curl use a user-agent string from your regular browser ("-A" or "--user-agent" option)? I've been doing that for a couple of years now, since nasdaq.com started rejecting the default lynx user agent.
It's rather easy in Perl if you use the LWP::UserAgent and HTML::TreeBuilder::XPath modules. It'd be just a few lines to fetch the page and then a few more to scrape the volume.
The only hard part would be guessing the right XPath formula. One way to express the location of the data is like this:
Code:
//tr[td[@data-snapfield="vol_and_avg"]]/td[2]
However, anything with XPath capabilities would do, not just perl.
Edit: that is for the Google source
Last edited by Turbocapitalist; 11-06-2017 at 12:54 PM.
Did you try just having curl use a user-agent string from your regular browser ("-A" or "--user-agent" option)? I've been doing that for a couple of years now, since nasdaq.com started rejecting the default lynx user agent.
opening the yahoo link in a mozilla browser displays a clear message.
i don't see how adding a useragentstring to curl would help.
The original post indicated a problem downloading from a script and did not mention any issue with using a browser. Thus, my belief that the user-agent might be the problem, as was indeed the case on nasdaq.com.
The original post indicated a problem downloading from a script and did not mention any issue with using a browser. Thus, my belief that the user-agent might be the problem, as was indeed the case on nasdaq.com.
Hmm. I tried a few browsers and all were served this by Yahoo:
"It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com."
So I figure using Yahoo as a data source is a lost cause. The HTML in the corresponding Google page is quite easy to work with using the tools perl has for XPath and fetching web pages.
Code:
#!/usr/bin/perl
use LWP::UserAgent;
use HTML::TreeBuilder::XPath;
use strict;
use warnings;
my $ua = LWP::UserAgent->new;
$ua->agent('dmchess-scraper/0.1 ');
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://finance.google.com/finance?q=F');
die("Failed : $response->status_line\n")
if ( ! $response->is_success);
my $contents = $response->decoded_content;
my $xhtml = HTML::TreeBuilder->new;
$xhtml->parse( $contents )
or die( "Could not parse HTML : $! \n");
my ( $volume )
= ( $xhtml->findnodes( '//tr[td[@data-snapfield="vol_and_avg"]]/td[2]' ) );
my ( $vol, $avg ) = split(/\//,$volume->as_trimmed_text());
print qq(OK\t$vol, $avg\n);
exit(0);
The original post indicated a problem downloading from a script and did not mention any issue with using a browser. Thus, my belief that the user-agent might be the problem, as was indeed the case on nasdaq.com.
had you clicked on the link, you'd have found this:
Quote:
Originally Posted by Turbocapitalist
Hmm. I tried a few browsers and all were served this by Yahoo:
"It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com."
The original post indicated a problem downloading from a script and did not mention any issue with using a browser. Thus, my belief that the user-agent might be the problem, as was indeed the case on nasdaq.com.
Quote:
Originally Posted by ondoho
had you clicked on the link, you'd have found this:
Quote:
Originally Posted by Turbocapitalist
Hmm. I tried a few browsers and all were served this by Yahoo:
"It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com."
that was my point.
Change "clicked on the link" to "copied and pasted the link into a browser".
The only clickable link in that original post was to google.com. That curl command did not appear to be complete as it did not request any particular ticker symbol(s).
Change "clicked on the link" to "copied and pasted the link into a browser".
The only clickable link in that original post was to google.com. That curl command did not appear to be complete as it did not request any particular ticker symbol(s).
./Stock.sh F
Can't locate HTML/TreeBuilder/XPath.pm in @INC (you may need to install the HTML::TreeBuilder::XPath module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at ./Stock.sh line 4.
BEGIN failed--compilation aborted at ./Stock.sh line 4.
I first tried installing XPath
sudo apt-get install libxml-xpath-perl
That didn't work so I googled around and some one said I had to run cpan command, so I tried that
sudo cpan XML::XPath
And I am still getting:
./Stock.sh F
Can't locate HTML/TreeBuilder/XPath.pm in @INC (you may need to install the HTML::TreeBuilder::XPath module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at ./Stock.sh line 4.
BEGIN failed--compilation aborted at ./Stock.sh line 4.
I am not knowledgeable on perl. I don't normally use it. I am ok with awk, pascal or c, but not perl.
Can't locate HTML/TreeBuilder/XPath.pm in @INC (you may need to install the HTML::TreeBuilder::XPath module)
That would be libhtml-treebuilder-xpath-perl in your repository. The names do matter a lot.
There is also a generic XML Xpath module, but it's not relevant here. Sine you are dealing with HTML you'll need one that can handle all the mistakes found even today in HTML. Thank M$ for that and Netscape for playing along with M$ in their 'browser war' that went on for so long starting over 20 years ago. It ended but we're still paying the cost for that with lots of non-standard, hard-to-parse, pseudo HTML.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.