//================================================================C // Web(http)から株価データを入手 (入力と同じ形で出力) // Http(Input): http://table.yahoo.co.jp/t // 株価コード(nnnn)を指定する // Output File: M-nnnn.txt : プログラムと同じディレクトリ //----------------------------------------------------------------C // 取得月数の最大は50ヶ月 // Start date : y1 = 2007; m1 = 1; // End date : year = 2010; month = 12; //----------------------------------------------------------------C // javac HttpRead1.java : classファイルの作成 // java HttpRead1 : 実行 //----------------------------------------------------------------C // Written by Yasunori Ushiro (後 保範), 2011/10/31 // ( Waseda University, 早稲田大学 ) //================================================================C import java.net.*; import java.io.*; import java.util.*; public class HttpRead1 { public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { String[] Ary, Wk; String input, str, str1; String[] Ar1, Ar2; String small = ""; int count=1; int year, month, y1, m1, cy, cm, nmonth, end = 0; // Set start and end date (year, month) y1 = 2007; m1 = 1; year = 2010; month = 12; nmonth = (year - y1)*12 + month - m1 + 1; // Input stock-code System.out.println("株価コード(4桁の数字)を入力"); InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); String code = br.readLine(); // Http Connection and Input Buffer cy = year; cm = month; URL url = new URL("http://table.yahoo.co.jp/t?"+ "c="+y1+"&a="+m1+"&b=1"+"&f="+year+"&d="+month+ "&e=31"+"&g=m&s="+code+"&y=0&z=&x=sb"); HttpURLConnection set = (HttpURLConnection)url.openConnection(); set.setRequestProperty("Accept-Language", "jp"); set.connect(); BufferedReader fi = new BufferedReader(new InputStreamReader(set.getInputStream())); // Output File FileOutputStream fo = new FileOutputStream("M-"+code+".txt"); PrintWriter q = new PrintWriter(fo,true); // Http File Read while (true){ input = fi.readLine(); if ( input == null ){ break; } if ( input.startsWith("") ) { if ( end == 1) { break; } else { end = 1; } } // 株価データ if ( input.startsWith("") ) { if ( input.indexOf(small) != -1 ) // 最初の6データは株価以外、最後の数データも株価以外 { if ( count >= 7 ) { Wk = input.split(small); Ary = Wk[1].split(""); int ct7 = count % 7; // xxxx年yy月 or xxxx年y月 if ( ct7 == 0 ) { if ( cm >= 10) { q.print(cy+"/"+cm+" "); } else { q.print(cy+"/0"+cm+" "); } // cy is current year, cm is current month if ( cm != 1 ) { cm--; } else { cm = 12; cy--; } } // cs7=1 --> 始値, 2-->高値, 3-->安値, 6-->終値 Ar1 = Ary[0].split(","); String st = Ar1[0]; for (int i=1; i