`
xiangxingchina
  • 浏览: 509008 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java抓取天气预报数据

阅读更多

这个东西虽然简单,但还是挺好玩的:首先把搜索后的页面用流读取出来,再写个正则,去除不要的内容,再把最后的结果存成xml格式文件、或者直接存入数据库,用的时候再调用

本代码只是显示html也的源码内容,如果需要抽取内容请自行改写public static String regex()中的正则式

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.     }  

分享到:
评论
1 楼 hehongwei44 2012-05-17  
不错  谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics