浏览 3366 次
锁定老帖子 主题:动态抓取网站股票信息
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-11-15
<% 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); } 应该还有一些更好的方法,如那位朋友知道有更好的方法请吝赐教,不胜感激。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |