2017-04-20 13 views
0

私はyahoo financeから現在および過去の株価を要求するJavaソフトウェアを持っています。Javaスキャナが特定のファイルを読み取らない

他の投稿に記載されているように、yahooは価格をファイルに書き込むことができます。ファイルはスキャナで読み取ることができます。 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

私は私のブラウザでは、これら2つのリンクをアクセスした場合、それは.CSVをダウンロードし、私は呼んでアマゾンの現在の価格を要求すること それぞれの期待されるデータを含むファイル。要求は、上記のいずれかのリンクである

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; 
} 

Javaで

対応するCSVファイルはfolling方法で読まれるべきです。

私はこのソフトウェアを数週間使用していて、完璧に動作しました。 しかし、それは過去のデータではもう機能しませんが、現在のデータでは問題なく動作します。

履歴データへのリンクを使用すると、スキャナは正常に開き、例外はスローされませんが、scanner.hasNextLine()は即座にfalseを返しますが、ブラウザにダウンロードされた.csvファイルは1305行です。

スキャナがヒストリカルデータの.csvファイルを受け入れないが、現在のデータを受け入れる理由を理解している人はいますか?

答えて

1

あなたのURLを「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を取り戻す)が、から生成された入力ストリーム古いURLは空になります。ブラウザの動作をエミュレートするには、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(); 
     } 
    } 
} 
関連する問題