2017-04-20 37 views
0

我有一个Java软件,它要求雅虎财务的当前和历史股票价格。Java扫描器不读取特定文件

正如其他文章所述,雅虎可以将价格写入一个文件中,这个文件可以被扫描器读取。 要请求亚马逊目前的价格我打电话: http://finance.yahoo.com/d/quotes.csv?s=AMZ.DE&f=snl1t1c4

要请求亚马逊的价格在过去的5年里,我呼吁: http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

如果我访问我的浏览器这两个环节,它的下载为.csv文件中包含他们每个人的预期数据。

在java中相应.csv文件应由folling方法被读取:

private static List<String> requestStockData(String request) throws IOException { 
    Scanner scanner = null; 
    List<String> answer = new ArrayList(); 
    try { 
     scanner = new Scanner(new URL(request).openStream());//no exception here 
    } catch (FileNotFoundException e) { 
     Tools.printDebugMessage("Received null-answer for request " + request); 
     return answer; 
    } 
    while (scanner.hasNextLine()) {//scanner.hasNextLine() returns false 
     String value = scanner.nextLine(); 
     answer.add(value); 
     Tools.printDebugMessage("received answer from YAHOO! Finance: " + value); 
    } 
    scanner.close(); 
    return answer; 
} 

其中请求是上面的链接中的一个。

我正在使用这个软件几个星期,它的工作完美。 但最后一天它不再适用于历史数据,但它对当前数据正常工作。

使用指向历史数据的链接,扫描程序将正常打开并且不会抛出异常,但scanner.hasNextLine()将立即返回false,但通过浏览器下载的.csv文件有1305行。

您是否有人理解为什么扫描仪不再接受历史数据的.csv文件,但接受当前的数据?

回答

1

使用“https”更新您的网址,然后重试。

老:http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

新:https://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

received answer from YAHOO! Finance: Date,Open,High,Low,Close,Volume,Adj Close 
received answer from YAHOO! Finance: 2017-04-19,844.95,849.35,842.90,847.90,1700,847.90 
received answer from YAHOO! Finance: 2017-04-18,849.50,851.00,841.25,845.00,3100,845.00 
received answer from YAHOO! Finance: 2017-04-17,839.90,839.90,839.90,839.90,000,839.90 
1

的原因是,当你调用http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs的浏览器重定向到https://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs(即你回到代码301),但是从产生的输入流旧网址将为空。如果您想模拟浏览器的功能,则必须发送HTTP请求,例如像这样:

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.HttpClients; 

public class Main { 
    public static void main(String [] args){ 
     String request = "http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs"; 
     try { 
      HttpGet httpget = new HttpGet(request);   
      HttpResponse response = HttpClients.createDefault().execute(httpget); 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 
      String filePath = "output.csv"; 
      FileOutputStream fos = new FileOutputStream(new File(filePath)); 
      int inByte; 
      while((inByte = is.read()) != -1) 
       fos.write(inByte); 
      is.close(); 
      fos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}