我正在编写一个程序来自动从网址下载数据。数据在csv中,我正在尝试将其转换为可用信息。下载部分工作正常,但是,我使用var_export然后将数据转换为数组,然后从数组中获取数据。问题是数据不是从数组中获取的,我不知道为什么。提前谢谢。
<?php ///without data harvesting
$loop = 1;
while ($loop < 401)
{
///data file harvesting csv
$symbol = array("A","AA","AAN","AAP","AAT","AAV","AB","ABB","ABBV","ABC","ABEV","ABG","ABM","ABR","ABR-A","ABR-B","ABT","ABW-B","ABX","ACC","ACCO","ACE","ACG","ACH","ACI","ACM","ACMP","ACN","ACO","ACP","ACRE","ACT","ACW","ADC","ADM","ADS","ADT","ADX","AEB","AEC","AED","AEE","AEF","AEG","AEH","AEK","AEL","AEM","AEO","AEP","AER","AES","AES-C","AET","AEV","AF","AF-C","AFA","AFB","AFC","AFG","AFL","AFM","AFQ","AFSD","AFT","AFW","AG","AGC","AGCO","AGD","AGI","AGM","AGM-A","AGM.A","AGN","AGO","AGO-B","AGO-E","AGO-F","AGRO","AGU","AGX","AH","AHC","AHH","AHL","AHL-A","AHL-B","AHL-C","AHP","AHS","AHT","AHT-A","AHT-D","AHT-E","AI","AIB","AIF","AIG","AIG.W","AIN","AIR","AIT","AIV","AIV-Z","AIW","AIY","AIZ","AJG","AKO.A","AKO.B","AKP","AKR","AKS","AL","ALB","ALDW","ALE","ALEX","ALG","ALJ","ALK","ALL","ALL-A","ALL-B","ALL-C","ALLE","ALP-N","ALP-O","ALP-P","ALR","ALR-B","ALSN","ALU","ALV","ALX","AMC","AMD","AME","AMG","AMH","AMH-A","AMID","AMP","AMP-A","AMRC","AMRE","AMT","AMT-A","AMTD","AMTG","AMX","AN","ANF","ANFI","ANH","ANH-A","ANH-B","ANN","ANR","ANW","AOD","AOI","AOL","AON","AOS","AP","APA","APAM","APB","APC","APD","APF","APH","APL","APO","APU","AR","ARC","ARCO","ARCX","ARDC","ARE","ARE-E","ARG","ARH-C","ARI","ARI-A","ARK","ARL","ARMF","ARMK","ARN","ARO","ARP","ARPI","ARR","ARR-A","ARR-B");
$symboluse = $symbol[$loop];
$historical_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=d&ignore=.csv');
$dividend_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=v&ignore=.csv');
$general_data_1 = file_get_contents('http://finance.yahoo.com/d/quotes.csv?s='.$symboluse.'&f=e7e8rr5');
///converting csv to usable data
///historical
$h_array =
$historical_array = array (1447, explode(',', $historical_data_1), true );
$dividend_array = array ( explode(',', $dividend_data_1, true) );
$general_array = array ( explode(',', $general_data_1, true) );
var_dump($historical_array);
var_dump($dividend_array);
var_dump($general_array);
///making it usable data
$symbol_1 = $symboluse;
$price = intval($historical_array[11]); /// have to call the array inside an array because the data doesn't collect
$dividend1 = intval($dividend_array[2]);
$dividend2 = intval($dividend_array[3]);
$dividend3 = intval($dividend_array[4]);
$dividend4 = intval($dividend_array[5]);
$PE_Ratio = intval($general_array[2]);
$EPS = intval($general_array[0]);
$EPS_next_year = intval($general_array[1]);
$PEG_Ratio = intval($general_array[3]);
echo $symbol_1."\n";
echo $price."\n";
echo $dividend1."\n";
echo $dividend2."\n";
echo $dividend3."\n";
echo $dividend4."\n";
echo $PE_Ratio."\n";
echo $EPS."\n";
echo $EPS_next_year."\n";
echo $PEG_Ratio."\n";
$loop++;
}
?>
答案 0 :(得分:0)
这里有很多错误。使用explode()
分割CSV文件仅适用于某些情况。在这里,您有多行需要先分开的文件。使用str_getcsv()
会更可靠。
此外,您从结果数组的错误元素中获取数据
最后,您将浮点数据转换为intval()
的整数,因此您将得到零结果。
试试这个版本,减少测试。你可以自己重建其余部分。
$symbol = array("A","AA");
foreach ($symbol as $symboluse) {
$dividend_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=v&ignore=.csv');
///converting csv to usable data
///historical
$Data = str_getcsv($dividend_data_1,"\n"); // separate lines
$dividend_array = [];
foreach($Data as $row) {
$dividend_array[] = str_getcsv($row); // parse individual lines.
}
// var_dump($dividend_array);
///making it usable data
$dividend1 = $dividend_array[1][1];
$dividend2 = $dividend_array[2][1];
$dividend3 = $dividend_array[3][1];
$dividend4 = $dividend_array[4][1];
echo $symboluse.' ';
echo $dividend1.", ";
echo $dividend2.", ";
echo $dividend3.", ";
echo $dividend4;
echo '<br>';
}