📄 querythestockid.java
字号:
package querystock;
import java.net.*;
import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class QueryTheStockId {
protected static final String _URL = "http://finance.cn.yahoo.com/fin/finance_search_result.html?s=";
protected static final String _TOKEN1 = "searbg3";
//protected static final String _TOKEN2 = "new_price";
//protected static final String _DELIMITER = "</>\"=";
protected static final String _DELIMITER = ":</?=.>\"=";
protected static boolean stocktypeAH=true;
public static String getLastStockid(String tickerSymbol)
throws WebsiteDataException, NoSuchTickerException {
String strURLStart = _URL;
URL urlWebPage = null;
InputStreamReader isr = null;
BufferedReader brWebPage = null;
// open the web page for reading
try {
urlWebPage = new URL(strURLStart + tickerSymbol);
isr = new InputStreamReader(urlWebPage.openStream());
brWebPage = new BufferedReader(isr);
} catch(Exception e) {
throw new WebsiteDataException();
}
// find the line with the stock quote on it
String strLine = null;
try {
while(true) {
strLine = brWebPage.readLine();
if(strLine == null)
{
throw new NoSuchTickerException("Invalid ticker symbol!");
}
if((strLine.indexOf(_TOKEN1) != -1))
break;
}
} catch(IOException e) {
throw new WebsiteDataException();
}
if(strLine.indexOf(_TOKEN1) != -1)
try {
for (int i = 7; i >= 1; i--)
strLine = brWebPage.readLine();
//System.out.println("A"+strLine);
} catch(IOException e) {
throw new WebsiteDataException();
}
// find the stock quote in the line
String strStockValue="";
StringTokenizer strtok = new StringTokenizer(strLine, _DELIMITER);
while(true) {
if(strtok.nextToken().compareTo("stock_id") == 0) {
//System.out.println("stock_id");
stocktypeAH = false;
break;
}
}
if(stocktypeAH==false){
strStockValue = strtok.nextToken("=\">.</") ;
//System.out.println(strStockValue);
}else{
strStockValue = null;
}
return strStockValue;
}
/*public static void main(String args[]){
try {
getLastStockid("NFHK");
getLastStockid("ZSYH");
getLastStockid("ZGLT");
getLastStockid("JSYH");
getLastStockid("ZGSH");
System.out.println();
//System.out.println(getNameFromTicker("NFHK"));
} catch (WebsiteDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchTickerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -