正则表达式学习 ---- 抓取远程网页并解析HTML

 

  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3. import org.apache.commons.httpclient.HttpClient;  
  4. import org.apache.commons.httpclient.HttpStatus;  
  5. import org.apache.commons.httpclient.methods.GetMethod;  
  6.   
  7. public class HttpClientDemo {  
  8.       
  9.     /** 
  10.      *  
  11.      * @param url 
  12.      * @return 
  13.      * @throws Exception 
  14.      */  
  15.     public static String getHTML(String url) throws Exception {  
  16.         HttpClient httpClient = new HttpClient();  
  17.         GetMethod getMethod = new GetMethod(url);  
  18.         int statusCode = httpClient.executeMethod(getMethod);  
  19.         if (statusCode != HttpStatus.SC_OK) {  
  20.             System.err.println("Method failed: " + getMethod.getStatusLine());  
  21.             return null;  
  22.         }  
  23.         // 读取内容  
  24.         byte[] responseBody = getMethod.getResponseBody();  
  25.         getMethod.releaseConnection();  
  26.         return new String(responseBody);  
  27.   
  28.     }  
  29.     /** 
  30.      *  
  31.      * @throws Exception 
  32.      */  
  33.     public static void test(String url) throws Exception{  
  34.           
  35.         String html = getHTML(url);  
  36.         Pattern p = null;  
  37.         Matcher m = null;  
  38.         StringBuffer sb0 = new StringBuffer();  
  39.         // ul正则  
  40.         String regex = "<ul class=\"d2_9\">([\\s\\S]*<li>)<a.*href='(.*)'.*>(.+?)</a> \\[(.*)\\]</li>([\\s].*)";  
  41.         // 链接正则  
  42.         String regexa = "<a.*href='(.*)'.*>(.+?)</a> \\[(.*)\\]";  
  43.         p = Pattern.compile(regex);  
  44.         // m = p.matcher(sb.toString());  
  45.         m = p.matcher(html);  
  46.         int count = 0;  
  47.         // ul字符串  
  48.         while (m.find()) {  
  49.             sb0.append(m.group());  
  50.         }  
  51.         //System.out.println(sb0.toString());  
  52.         p = Pattern.compile(regexa);  
  53.         m = p.matcher(sb0.toString());  
  54.         // 链接地址和标题  
  55.         while (m.find()) {  
  56.             System.out.println("地址:" + m.group(1));  
  57.             System.out.println("标题:" + m.group(2));  
  58.             System.out.println("时间:" + m.group(3));  
  59.             count++;  
  60.         }  
  61.           
  62.         System.out.println("抓取条数:"+count);  
  63.   
  64.     }  
  65.       
  66.     public static void main(String[] args) throws Exception {  
  67.         String url = "http://cpc.people.com.cn/GB/194302/194306/index.html";  
  68.         test(url);  
  69.           
  70.     }  
  71. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值