java 正则表达式 抓取网页中的新闻

  1. package rssTest;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.MalformedURLException;  
  8. import java.net.URL;  
  9. import java.net.URLConnection;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12. import java.util.regex.Matcher;  
  13. import java.util.regex.Pattern;  
  14.   
  15. /** 
  16.  * @author Der 
  17.  * @date   05-01 
  18.  * @E-mail uidin@163.com 
  19.  * */  
  20. public class MyRSS  
  21. {  
  22.     /** 
  23.      * 获取搜索结果的html源码 
  24.      * */  
  25.     public static String getHtmlSource(String url)  
  26.     {  
  27.           
  28.         StringBuffer codeBuffer = null;  
  29.         BufferedReader in=null;  
  30.         try  
  31.         {  
  32.             URLConnection uc = new URL(url).openConnection();  
  33.   
  34.             /** 
  35.              * 为了限制客户端不通过网页直接读取网页内容,就限制只能从浏览器提交请求. 
  36.              * 但是我们可以通过修改http头的User-Agent来伪装,这个代码就是这个作用 
  37.              *  
  38.              */  
  39.             uc.setRequestProperty("User-Agent",  
  40.                     "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");  
  41.   
  42.             // 读取url流内容  
  43.             in = new BufferedReader(new InputStreamReader(uc  
  44.                     .getInputStream(), "gb2312"));  
  45.             codeBuffer = new StringBuffer();  
  46.             String tempCode = "";  
  47.             // 把buffer内的值读取出来,保存到code中  
  48.             while ((tempCode = in.readLine()) != null)  
  49.             {  
  50.                 codeBuffer.append(tempCode).append("\n");  
  51.             }  
  52.             in.close();  
  53.         }  
  54.         catch (MalformedURLException e)  
  55.         {  
  56.             e.printStackTrace();  
  57.         }  
  58.         catch (IOException e)  
  59.         {  
  60.             e.printStackTrace();  
  61.         }  
  62.           
  63.         return codeBuffer.toString();  
  64.     }  
  65.   
  66.     /** 
  67.      * 正则表达式 
  68.      * */  
  69.     public static String regex()  
  70.     {  
  71.         String googleRegex = "<div class=g>(.*?)href=\"(.*?)\"(.*?)\">(.*?)</a>(.*?)<div class=std>(.*?)<br>";  
  72.         return googleRegex;  
  73.     }  
  74.   
  75.     /** 
  76.      * 测试用 
  77.      * 在google中检索关键字,并抽取自己想要的内容 
  78.      *  
  79.      * */  
  80.     public static List<String> GetNews()  
  81.     {  
  82.         List<String> newsList = new ArrayList<String>();  
  83.         String allHtmlSource = MyRSS  
  84.                 .getHtmlSource("http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&client=aff-os-maxthon&hs=SUZ&q=%E8%A7%81%E9%BE%99%E5%8D%B8%E7%94%B2&meta=&aq=f");  
  85.         Pattern pattern = Pattern.compile(regex());  
  86.         Matcher matcher = pattern.matcher(allHtmlSource);  
  87.   
  88.         while (matcher.find())  
  89.         {  
  90.             String urlLink = matcher.group(2);  
  91.             String title = matcher.group(4);  
  92.             title = title.replaceAll("<font color=CC0033>""");  
  93.             title = title.replaceAll("</font>""");  
  94.             title = title.replaceAll("<b>...</b>""");  
  95.   
  96.             String content = matcher.group(6);  
  97.             content = content.replaceAll("<font color=CC0033>""");  
  98.             content = content.replaceAll("</font>""");  
  99.             content = content.replaceAll("<b>...</b>""");  
  100.   
  101.             newsList.add(urlLink);  
  102.             newsList.add(title);  
  103.             newsList.add(content);  
  104.         }  
  105.         return newsList;  
  106.     }  
  107.   
  108.     /** 
  109.      * main方法 
  110.      * */  
  111.     public static void main(String[] args)  
  112.     {  
  113.         System.out  
  114.         .println(MyRSS  
  115.                 .getHtmlSource("http://main.house.sina.com.cn/news/zckb/index.html"));  
  116.     }  
  117. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值