论坛首页 Java企业应用论坛

动态抓取网站股票信息

浏览 3366 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-15  
客户的网站要有一个能动态显示自已股票信息的模块,一般都不愿意花钱来买接口,这样的话就只能新增一个protlet从别的网站上来抓取信息了,至于如何新增portlet配置文件这里就不说了,主要说一下JSP、JAVA关键的代码。JSP代码如下:
<%
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>&nbsp;");
cdStockChangeValue = Gfun.getSubString(sTotalString,
":<span id=\"lblUpDown\"><font color=\"#185493\">", "&nbsp;<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>&#x5EFA;&#x53D1;&#x80A1;&#x4EFD;&nbsp;<%=cdStockValue%>&nbsp;
<%if(cdStockvalueRate>0){ %><font color="red" size="10"><b>&#x2191;</b></font>
<font color="red"><%=cdcStockValueRateStr%>%</font>
<%}else if(cdStockvalueRate<0){ %><font color="#006600" size="10"><b>&#x2193;</b></font>
<font color="#006600"><%=cdcStockValueRateStr%>%</font>
<%} %>
</td></tr>
<tr> <td>&#x4E0A;&#x8BC1;&#x6307;&#x6570;&nbsp;<%=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企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics