Hello,
I'm trying to extract data from Google finance csv file using javasript. http://finance.google.com/finance/hi...=25&output=csv
however the csv file has 6 columns (Date,Open,High,Low,Close,Volume),
and i want only two columns (Date,Close) in array from it.
Following is the example of csv - Date,Open,High,Low,Close,Volume
-
5-May-09,399.98,405.00,397.25,402.99,2401096
-
4-May-09,398.17,402.40,394.79,401.98,3204843
-
1-May-09,395.03,397.59,391.55,393.69,2428611
-
30-Apr-09,395.76,403.75,394.80,395.97,4361069
I wrote following code according to http://bytes.com/topic/javascript/an...ascript-arrays
but i'm getting errors in the csvArray() method, 'undefined' is null or not an object. - <html>
-
<head>
-
<title>- Another CSV parser -</title>
-
<script type="text/javascript" src="jquery.js"></script>
-
<script type="text/javascript">
-
function loadData(){
-
$.get("http://finance.google.com/finance/historical",
-
{
-
q:'GOOG',
-
startdate:'May 1 2009',
-
enddate:'May 5 2009',
-
start:'225',
-
num:'25',
-
output:'csv'
-
},
-
function(data)
-
{
-
// Writing output into div
-
$("#csvDiv").html(data);
-
$("#csvDiv").hide();
-
ConvertToArray(data);
-
}
-
);
-
}
-
function csvArray(csv){
-
var i= 0;var A= csv.split(/\s*\n\s*/);
-
while(A[i++]) A[i]= A[i].split(/ *, */);
-
return A;
-
}
-
function ConvertToArray(data){
-
var myStr=$("#csvDiv").html();
-
var myArr=csvArray(myStr);
-
//ignore first 6 lines
-
var i=6;
-
for (i; i<myArr.length; i++) {
-
document.writeln(myArr[i]+'<br />');
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<div id="csvDiv">
-
-
</div>
-
<script type="text/javascript">
-
loadData();
-
</script>
-
</body>
-
</html>
regards,
Nitin Sawant
13 11653
I'm also unable to replace newline character with comma -
myArr=myArr.replace("\n",",");
-
Hi I am litlle bit new to jquery. But i got some doubts/not clear with your code.. - function(data)
-
{
-
// Writing output into div
-
$("#csvDiv").html(data);
-
$("#csvDiv").hide();
-
ConvertToArray(data);
-
}
In the above mentioned function, from were u are getting the value for the variable data? when i alerted that value its seems o be a html file. But it returns empty value. My suggestion is that u get an undefined error in the function csvArray() as it gets null value.
Regards
Ramanan Kalirajan
Hello Raman,
Thanks for your reply,
$.get() function returns the variable data,
There is no problem retrieving csv string,
You can see the retrieved data by commenting $("#csvDiv").hide();
Manipulating csv string data is quite difficult for me.
I want to convert it to javascript array,
regards,
Nitin Sawant
Check that myStr is defined.
When using the newline character in a string, you need to escape it: - myArr=myArr.replace("\\n",",");
Hello,
Thanks for your reply,
Still it doesn't work, i think google people have inserted some secret character instead of newline in the csv file. -
<html>
-
<head>
-
<title>- Another CSV parser -</title>
-
<script type="text/javascript" src="flotr/jquery.js"></script>
-
<script type="text/javascript">
-
function loadData(){
-
$.get("http://finance.google.com/finance/historical",
-
{
-
q:'GOOG',
-
startdate:'May 1 2009',
-
enddate:'May 5 2009',
-
start:'225',
-
num:'25',
-
output:'csv'
-
},
-
function(data)
-
{
-
// Writing output into div
-
$("#csvDiv").html(data);
-
$("#csvDiv").hide();
-
ConvertToArray(data);
-
}
-
);
-
}
-
-
function ConvertToArray(data){
-
var myArr=$("#csvDiv").html();
-
myArr=myArr.replace('\\n',',');//this line doesn't work
-
myArr=myArr.split(',');
-
document.writeln("array length is: "+myArr.length+"<br/>");
-
-
//show all elements of array
-
var i=0;
-
for (i; i<myArr.length; i++) {
-
document.writeln(myArr[i]+'<br />');
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<div id="csvDiv">
-
-
</div>
-
<script type="text/javascript">
-
loadData();
-
</script>
-
<!--
-
http://finance.google.com/finance/historical?q=GOOG&startdate=May+1%2C+2009&enddate=May+5%2C+2009&start=225&num=25&output=csv
-
-->
-
</body>
-
</html>
It doesn't separate the stock volume and date -
Date
-
Open
-
High
-
Low
-
Close
-
Volume 5-May-09
-
399.98
-
405.00
-
397.25
-
402.99
-
2401096 4-May-09
-
398.17
-
402.40
-
394.79
-
401.98
-
3204843 1-May-09
-
395.03
-
397.59
-
391.55
-
393.69
-
2428611 30-Apr-09
-
395.76
-
403.75
-
394.80
-
395.97
-
4361069
It's probably because the script is in another domain, not yours. Use server-side code to get it from your domain or use JSON/script tags.
@acoder
I'm able to access data but unable to manipulate it,
There must be some problem with csv file or google has locked it so that no one can retrieve it
In your original code, on line 31, what's the value of myStr?
value of myStr is the content of csv file returned from Google Finance site: -
Date,Open,High,Low,Close,Volume 5-May-09,399.98,405.00,397.25,402.99,2401096 4-May-09,398.17,402.40,394.79,401.98,3204843 1-May-09,395.03,397.59,391.55,393.69,2428611 30-Apr-09,395.76,403.75,394.80,395.97,4361069
Instead of using the HTML from the csv Div, use the 'data' directly: - var myArr=csvArray(data);
I'm not sure how you're getting the data if it's from a different domain. Are you testing locally?
Thanks a million @acoder
when I was testing it locally, it was working but
when i put it on server it was not working.
there is something called as cross site restriction with javascript, thats why not working
now i resolved the error,
its becoming too complicated in javascript so i did it through screen scraping in php. now its working as desired http://nitin.tech4you.org/retrieve.php -
<?php
-
/*
-
* example.php
-
* class_http.php example usage
-
* Author: Troy Wolf (troy@troywolf.com)
-
* Comments: Please be a good neighbor when screen-scraping. Don't write code
-
that will needlessly make hits to third-party websites. Use
-
class_http's caching feature whenever possible. It is designed to
-
make you a good neighbor!
-
*/
-
-
/*
-
Include the http class. Modify path according to where you put the class
-
file.
-
*/
-
require_once(dirname(__FILE__).'/class_http.php');
-
-
/* First, instantiate a new http object. */
-
$h = new http();
-
-
/*
-
Where do you want to store your cache files?
-
Default is current dir. You can set it here, or hard-code in the class.
-
You must end this value with a "/".
-
*/
-
//$h->dir = "/public_html/";
-
-
-
$currentDate= date("d-M-y",time());
-
-
/*
-
Screen-scrape the Google home page without caching.
-
*/
-
if (!$h->fetch('http://finance.google.com/finance/historical?q=GOOG&startdate=May+1%2C+2009&enddate='.$currentDate.'&start=225&num=25&output=csv',60)) {
-
/*
-
The class has a 'log' property that contains a log of events. This log is
-
useful for testing and debugging.
-
*/
-
echo "<h2>There is a problem with the http request!</h2>";
-
echo $h->log;
-
exit();
-
}
-
-
/* Echo out the body content fetched from the url. */
-
$content= $h->body;
-
-
//replace all newlines
-
$content=str_replace("\n",",",$content);
-
echo $content;
-
-
/* If you just want to know the HTTP status code: */
-
//echo "Status: ".$h->status;
-
-
/* If you are interested in seeing all the response headers: */
-
//echo "<pre>".$h->header."</pre>";
-
-
-
?>
-
Thanks again @Raman, @Acoder
warm regards,
Nitin Sawant
That explains it. Thanks for posting your solution.
There is a powerful Google Finance scraper which can scrape companies, exchanges and price information large scaled.
It's a free PHP project at http://scrape-google-finance.compunect.com
Should help if you need to go for larger amounts of data.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Karthik |
last post by:
Hello,
How can I convert a BSTR data type to std::string??? Thanks a much!
Karthik
|
by: vsk |
last post by:
Hai,
I need to know how to convert an unsigned char array into hexstring.
can anyone help me in this regard?.
Thanks.
|
by: kvicky |
last post by:
I have multiline textbox and I am trying to do a search functionality
based on the input entered in this Textbox. I am able to convert into a
string array if the search components are separated by...
|
by: Bilwin |
last post by:
Hi friends,
How can i convert StringTokenizer to string array?
friends please reply me..
|
by: Maxim |
last post by:
Hi all,
I'm calling a COM Interface method that returnes SafeArray wrapped into
variant. Is it possible to convert it to managed array? Because working with
SAFEARRAY directly is a bit...
|
by: probashi |
last post by:
I would like to add Chart Like Google Finance into my ASPX pages.
How involved this will be !!!
Thanks.
|
by: mwenz |
last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them.
Code to instantiate the Access database and table...
Dim conn As New...
|
by: Man4ish |
last post by:
HI ,
I am trying to convert string into char array of characters.but facing problem.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string t="1,2,3,4,5,6";
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |