posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

扒网页数据(jdk+正则解析)

Posted on 2008-03-09 10:46 G_G 阅读(1490) 评论(2)  编辑  收藏 所属分类: javaGeneral
数据扒出效果
双色球(2008001=02,04,07,09,14,29#03
双色球(2008002=03,04,18,22,25,29#09
..

junit代码
package test;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import junit.framework.TestCase;

public class HttpConn extends TestCase {
    
public void testT() throws Exception {
        zq :
        
for(int i=2008001;true;i++){
            String num 
=  getQihao(i) ;
            System.out.println(
"双色球("+i+")="+ num);
            
if(num==null||num.equals("")) break zq;
        }
    }
    
    
public String getQihao(int qihao) throws Exception {
        URL url 
= new URL("http://www.cnlot.net/ssq/details.php?issue="+qihao);
        URLConnection uconn 
= url.openConnection();
        
        String num 
= "";
        
        InputStream in 
= uconn.getInputStream();
        
byte[] bs = new byte[in.available()];
        in.read(bs);
        String date 
= new String(bs) ;
        
        Pattern pa 
= Pattern.compile(" .+color=red>([0-9][0-9])<.+" );
        Matcher m 
= pa.matcher(date);
        
while( m.find() )
            num
+= m.group(1)+",";
        
         pa 
= Pattern.compile(" .+color=blue>([0-9][0-9])<.+" );
         m 
= pa.matcher(date);
        
while( m.find() )
            num 
= num.substring( 0,num.length()-1 )+"#"+m.group(1) ;
        
        pa 
= Pattern.compile("^(([0-9][0-9],){5,}[0-9][0-9]#([0-9][0-9],)*[0-9][0-9]\\|)*(([0-9][0-9],){5,}[0-9][0-9]#([0-9][0-9],)*[0-9][0-9])*$");
        m 
=  pa.matcher(num);
        
if( m.find() )
            
return num ;
        
else 
            
return null ;
    }
}



评论

# re: 扒网页数据(jdk+正则解析)  回复  更多评论   

2008-03-10 10:08 by richardning
呵呵,老大催我干活。回头详看。。我自己也写了个抓网络数据的代码,呵,有空。比较比较。

# re: 扒网页数据(jdk+正则解析)  回复  更多评论   

2008-03-11 10:18 by 蒋家狂潮
不错的文章,

只有注册用户登录后才能发表评论。


网站导航: