股票信息提取(获取/抓取)

<% 
String url = "http://info.cmbchina.com/cmb2005stock/investaspxbin/stock/SingleStkHQ.aspx?channel=SSHQ&&stockid=股票ID"; 
String sTotalString = Gfun.getWebInfo(url); 
String cdStockValue = ""; 
String cdStockChangeValue = ""; 
cdStockValue = Gfun.getSubString(sTotalString, 
":<font color=\"#185493\">", "</font> "); 
cdStockChangeValue = Gfun.getSubString(sTotalString, 
":<span id=\"lblUpDown\"><font color=\"#185493\">", " <img src="); 
float cdStockvalueRate =0; 
if(!cdStockValue.equals("0.00")){ 
cdStockvalueRate = new Float(cdStockChangeValue).floatValue()/new Float(cdStockValue).floatValue(); 
cdStockvalueRate = cdStockvalueRate * 100; 
} 
String cdcStockValueRateStr = String.valueOf(cdStockvalueRate); 
cdcStockValueRateStr = cdcStockValueRateStr.substring(0,cdcStockValueRateStr.indexOf(".")+2); 
String shStockValue = ""; 
sTotalString = Gfun.getWebInfo("http://info.cmbchina.com/cmb2005stock/investaspxbin/main/stockMarket.aspx"); 
shStockValue = Gfun.getSubString(sTotalString,"</td><td align=\"Right\">","</td><td align="); 
%> 
<div align="left" valign="middle"> 
<table width="100%" border="0" cellspacing="2" cellpadding="0" > 
<tr> 
<td>建发股份 <%=cdStockValue%>  
<%if(cdStockvalueRate>0){ %><font color="red" size="10"><b>↑</b></font> 
<font color="red"><%=cdcStockValueRateStr%>%</font> 
<%}else if(cdStockvalueRate<0){ %><font color="#006600" size="10"><b>↓</b></font> 
<font color="#006600"><%=cdcStockValueRateStr%>%</font> 
<%} %> 
</td></tr> 
<tr> <td>上证指数 <%=shStockValue%></td> </tr> 
</table></div> 
这里面主要取一个公司的当前股价和变化值,涨幅,还有沪市指数。Gfun.getWebInfo方示代码如下: 
public static String getWebInfo(String url){ 
String sCurrentLine; 

String sTotalString; 

sCurrentLine=""; 

sTotalString=""; 

try{ 
java.io.InputStream l_urlStream; 

java.net.URL l_url = new java.net.URL(url); 

java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection(); 

l_connection.connect(); 

l_urlStream = l_connection.getInputStream(); 

java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream)); 

while ((sCurrentLine = l_reader.readLine()) != null) 

{ 

sTotalString+=sCurrentLine; 

} 
}catch(Exception e){ 
} 

return sTotalString; 
} 
Gfun.getSubString方法代码如下: 
public static String getSubString(String strSource, String strStart, String strEnd) 
{ 
if(strSource == null || strStart == null || strEnd == null) 
return null; 
int nTmp1 = strSource.indexOf(strStart); 
if(nTmp1 < 0) 
return null; 
String strTmp = strSource.substring(nTmp1); 

int nTmp2 = strTmp.indexOf(strEnd,strStart.length()); 
if(nTmp2 < 0) 
return null; 
else 
return strTmp.substring(strStart.length(), nTmp2); 
} 

 

你可能感兴趣的:(java,.net)