正则表达式分析网页,获得中央一电视节目列表(转载)

本文介绍了一种利用正则表达式从特定网址抓取并解析网页内容的方法。通过HTTP请求获取指定URL的内容后,采用GB2312编码方式将其转换为字符串形式,再运用自定义的splitByReg函数进行解析。该函数能够根据提供的正则表达式从字符串中抽取所需的数据。
String strUrl="http://www.cctv.com/tvguide/11/01/20061010/1.shtml";

    
byte[] pageHtml = HttpUtil.getPage(strUrl);
    
//将页面转成string
    String strHtml =  new String(pageHtml, "GB2312"); 
    String[][] ls
= null;
    ls 
= StringUtil.splitByReg(strHtml,"(//d{2}://d{2}://d{2})</font>.*<font >(.+)</font>.*</tr>//r//n<tr>");
    
for(int i=0;i<ls.length;i++)
    
{
        
//String[] ls1[] = StringUtil.splitByReg(ls[i],"");
        System.out.print(ls[i][0]+"##"+ls[i][1]); 
        System.out.println();
    }
/**通用正则表达式解析函数
     * splitByReg  
     * 
@param str 需要解析的字符串
     * 
@param regExp  匹配的正则表达式
     * 
@return 解析后字符串数组
     
*/

    
public static String[][] splitByReg(String str,String regExp) {
        Pattern sp 
= Pattern.compile(regExp);
        Matcher matcher 
= sp.matcher(str); 
        Vector
<Vector<String>> colInoput= new Vector<Vector<String>>();
        
while (matcher.find()) {
            Vector
<String> v = new Vector<String>();
            
for(int i=1;i <= matcher.groupCount();i++)
            
{

                v.add(matcher.group(i));
            }

            colInoput.add(v);
        }
  
        String[][] resultList 
=null;
        
if(colInoput.size()>0)     
           resultList
=new String[colInoput.size()][colInoput.get(0).size()];
        
for(int i=0;i< colInoput.size();i++)
        
{
            String[] kk 
= new String[colInoput.get(i).size()];
            colInoput.get(i).copyInto(kk);
            resultList[i] 
= kk;
        }

        
return resultList;
    }
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值