抓取csdn上的各类别的文章 (制作csdn app 二)

来源:互联网 发布:2016coc连弩升级数据 编辑:程序博客网 时间:2024/05/16 12:47

转载请表明出处:http://blog.csdn.NET/lmj623565791/article/details/23532797

这篇博客接着上一篇(Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架)继续实现接下来的功能,如果你想了解整个app的制作过程,你可以去看一下上一篇,当然如果你只对网页信息的抓取感兴趣,你可以直接阅读本篇博客。我会把app功能分解,尽可能的每篇之间的耦合度不会太高。

好了,开始进入正题。这篇内容我新建一个Java项目实现,一方面java调试比较方便,另一方面我会使用导入jar包的方式,把这个项目导入到android项目使用,大家如果在导jar方面没有经验,可以看下。

先看下项目结构:

      

定义了一个NewsBean对于app的每个ListView的Item,Constaint是个接口,存放了一些常量,还有就是一些辅助类。

NewsItem.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.bean;  
  2.   
  3. public class NewsItem  
  4. {  
  5.     private int id;  
  6.   
  7.     /** 
  8.      * 标题 
  9.      */  
  10.     private String title;  
  11.     /** 
  12.      * 链接 
  13.      */  
  14.     private String link;  
  15.     /** 
  16.      * 发布日期 
  17.      */  
  18.     private String date;  
  19.     /** 
  20.      * 图片的链接 
  21.      */  
  22.     private String imgLink;  
  23.     /** 
  24.      * 内容 
  25.      */  
  26.     private String content;  
  27.   
  28.     /** 
  29.      * 类型   
  30.      *  
  31.      */  
  32.     private int newsType;  
  33.   
  34.     public int getNewsType()  
  35.     {  
  36.         return newsType;  
  37.     }  
  38.   
  39.     public void setNewsType(int newsType)  
  40.     {  
  41.         this.newsType = newsType;  
  42.     }  
  43.   
  44.     public String getTitle()  
  45.     {  
  46.         return title;  
  47.     }  
  48.   
  49.     public void setTitle(String title)  
  50.     {  
  51.         this.title = title;  
  52.     }  
  53.   
  54.     public String getLink()  
  55.     {  
  56.         return link;  
  57.     }  
  58.   
  59.     public void setLink(String link)  
  60.     {  
  61.         this.link = link;  
  62.     }  
  63.   
  64.     public int getId()  
  65.     {  
  66.         return id;  
  67.     }  
  68.   
  69.     public void setId(int id)  
  70.     {  
  71.         this.id = id;  
  72.     }  
  73.   
  74.     public String getDate()  
  75.     {  
  76.         return date;  
  77.     }  
  78.   
  79.     public void setDate(String date)  
  80.     {  
  81.         this.date = date;  
  82.     }  
  83.   
  84.     public String getImgLink()  
  85.     {  
  86.         return imgLink;  
  87.     }  
  88.   
  89.     public void setImgLink(String imgLink)  
  90.     {  
  91.         this.imgLink = imgLink;  
  92.     }  
  93.   
  94.     public String getContent()  
  95.     {  
  96.         return content;  
  97.     }  
  98.   
  99.     public void setContent(String content)  
  100.     {  
  101.         this.content = content;  
  102.     }  
  103.   
  104.     @Override  
  105.     public String toString()  
  106.     {  
  107.         return "NewsItem [id=" + id + ", title=" + title + ", link=" + link + ", date=" + date + ", imgLink=" + imgLink  
  108.                 + ", content=" + content + ", newsType=" + newsType + "]";  
  109.     }  
  110.   
  111. }  

CommonException.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.bean;  
  2.   
  3. public class CommonException extends Exception  
  4. {  
  5.   
  6.     public CommonException()  
  7.     {  
  8.         super();  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public CommonException(String message, Throwable cause)  
  13.     {  
  14.         super(message, cause);  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.   
  18.     public CommonException(String message)  
  19.     {  
  20.         super(message);  
  21.         // TODO Auto-generated constructor stub  
  22.     }  
  23.   
  24.     public CommonException(Throwable cause)  
  25.     {  
  26.         super(cause);  
  27.         // TODO Auto-generated constructor stub  
  28.     }  
  29.       
  30. }  

Constaint.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.csdn;  
  2.   
  3. public interface Constaint  
  4. {  
  5.     public static final int NEWS_TYPE_YEJIE = 1;  
  6.     public static final int NEWS_TYPE_YIDONG = 2;  
  7.     public static final int NEWS_TYPE_YANFA = 3;  
  8.     public static final int NEWS_TYPE_CHENGXUYUAN = 4;  
  9.     public static final int NEWS_TYPE_YUNJISUAN = 5;   
  10.       
  11.   
  12. }  
DataUtil.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.csdn;  
  2.   
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. import com.zhy.bean.CommonException;  
  8.   
  9. public class DataUtil  
  10. {  
  11.   
  12.     /** 
  13.      * 返回该链接地址的html数据 
  14.      *  
  15.      * @param urlStr 
  16.      * @return 
  17.      * @throws CommonException 
  18.      */  
  19.     public static String doGet(String urlStr) throws CommonException  
  20.     {  
  21.         StringBuffer sb = new StringBuffer();  
  22.         try  
  23.         {  
  24.             URL url = new URL(urlStr);  
  25.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  26.             conn.setRequestMethod("GET");  
  27.             conn.setConnectTimeout(5000);  
  28.             conn.setDoInput(true);  
  29.             conn.setDoOutput(true);  
  30.   
  31.             if (conn.getResponseCode() == 200)  
  32.             {  
  33.                 InputStream is = conn.getInputStream();  
  34.                 int len = 0;  
  35.                 byte[] buf = new byte[1024];  
  36.   
  37.                 while ((len = is.read(buf)) != -1)  
  38.                 {  
  39.                     sb.append(new String(buf, 0, len, "UTF-8"));  
  40.                 }  
  41.   
  42.                 is.close();  
  43.             } else  
  44.             {  
  45.                 throw new CommonException("访问网络失败!");  
  46.             }  
  47.   
  48.         } catch (Exception e)  
  49.         {  
  50.             throw new CommonException("访问网络失败!");  
  51.         }  
  52.         return sb.toString();  
  53.     }  
  54.   
  55.       
  56.       
  57.   
  58. }  

URLUtil.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.csdn;  
  2.   
  3.   
  4. public class URLUtil  
  5. {  
  6.   
  7.   
  8.     public static final String NEWS_LIST_URL = "http://www.csdn.net/headlines.html";  
  9.     public static final String NEWS_LIST_URL_YIDONG = "http://mobile.csdn.net/mobile";  
  10.     public static final String NEWS_LIST_URL_YANFA = "http://sd.csdn.net/sd";  
  11.     public static final String NEWS_LIST_URL_YUNJISUAN = "http://cloud.csdn.net/cloud";  
  12.     public static final String NEWS_LIST_URL_ZAZHI = "http://programmer.csdn.net/programmer";  
  13.     public static final String NEWS_LIST_URL_YEJIE = "http://news.csdn.net/news";  
  14.   
  15.   
  16.     /** 
  17.      * 根据文章类型,和当前页码生成url 
  18.      * @param newsType 
  19.      * @param currentPage 
  20.      * @return 
  21.      */  
  22.     public static String generateUrl(int newsType, int currentPage)  
  23.     {  
  24.         currentPage = currentPage > 0 ? currentPage : 1;  
  25.         String urlStr = "";  
  26.         switch (newsType)  
  27.         {  
  28.         case Constaint.NEWS_TYPE_YEJIE:  
  29.             urlStr = NEWS_LIST_URL_YEJIE;  
  30.             break;  
  31.         case Constaint.NEWS_TYPE_YANFA:  
  32.             urlStr = NEWS_LIST_URL_YANFA;  
  33.             break;  
  34.         case Constaint.NEWS_TYPE_CHENGXUYUAN:  
  35.             urlStr = NEWS_LIST_URL_ZAZHI;  
  36.             break;  
  37.         case Constaint.NEWS_TYPE_YUNJISUAN:  
  38.             urlStr = NEWS_LIST_URL_YUNJISUAN;  
  39.             break;  
  40.         default:  
  41.             urlStr = NEWS_LIST_URL_YIDONG;  
  42.             break;  
  43.         }  
  44.   
  45.   
  46.         urlStr += "/" + currentPage;  
  47.           
  48.         return urlStr;  
  49.   
  50.   
  51.     }  
  52.   
  53.   
  54. }  

NewsItemBiz.java业务类

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.biz;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.jsoup.Jsoup;  
  7. import org.jsoup.nodes.Document;  
  8. import org.jsoup.nodes.Element;  
  9. import org.jsoup.select.Elements;  
  10.   
  11. import com.zhy.bean.CommonException;  
  12. import com.zhy.bean.NewsItem;  
  13. import com.zhy.csdn.DataUtil;  
  14. import com.zhy.csdn.URLUtil;  
  15.   
  16. /** 
  17.  * 处理NewItem的业务类 
  18.  * @author zhy 
  19.  *  
  20.  */  
  21. public class NewsItemBiz  
  22. {  
  23.     /** 
  24.      * 业界、移动、云计算 
  25.      *  
  26.      * @param htmlStr 
  27.      * @return 
  28.      * @throws CommonException  
  29.      */  
  30.     public List<NewsItem> getNewsItems( int newsType , int currentPage) throws CommonException  
  31.     {  
  32.         String urlStr = URLUtil.generateUrl(newsType, currentPage);  
  33.           
  34.         String htmlStr = DataUtil.doGet(urlStr);  
  35.           
  36.         List<NewsItem> newsItems = new ArrayList<NewsItem>();  
  37.         NewsItem newsItem = null;  
  38.   
  39.         Document doc = Jsoup.parse(htmlStr);  
  40.         Elements units = doc.getElementsByClass("unit");  
  41.         for (int i = 0; i < units.size(); i++)  
  42.         {  
  43.             newsItem = new NewsItem();  
  44.             newsItem.setNewsType(newsType);  
  45.   
  46.             Element unit_ele = units.get(i);  
  47.   
  48.             Element h1_ele = unit_ele.getElementsByTag("h1").get(0);  
  49.             Element h1_a_ele = h1_ele.child(0);  
  50.             String title = h1_a_ele.text();  
  51.             String href = h1_a_ele.attr("href");  
  52.   
  53.             newsItem.setLink(href);  
  54.             newsItem.setTitle(title);  
  55.   
  56.             Element h4_ele = unit_ele.getElementsByTag("h4").get(0);  
  57.             Element ago_ele = h4_ele.getElementsByClass("ago").get(0);  
  58.             String date = ago_ele.text();  
  59.   
  60.             newsItem.setDate(date);  
  61.   
  62.             Element dl_ele = unit_ele.getElementsByTag("dl").get(0);// dl  
  63.             Element dt_ele = dl_ele.child(0);// dt  
  64.             try  
  65.             {// 可能没有图片  
  66.                 Element img_ele = dt_ele.child(0);  
  67.                 String imgLink = img_ele.child(0).attr("src");  
  68.                 newsItem.setImgLink(imgLink);  
  69.             } catch (IndexOutOfBoundsException e)  
  70.             {  
  71.   
  72.             }  
  73.             Element content_ele = dl_ele.child(1);// dd  
  74.             String content = content_ele.text();  
  75.             newsItem.setContent(content);  
  76.             newsItems.add(newsItem);  
  77.         }  
  78.   
  79.         return newsItems;  
  80.   
  81.     }  
  82.   
  83. }  
好了,最后就是测试了,这里使用单元测试,下面是测试代码和结果。

测试代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.zhy.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.zhy.bean.CommonException;  
  6. import com.zhy.bean.NewsItem;  
  7. import com.zhy.biz.NewsItemBiz;  
  8. import com.zhy.csdn.Constaint;  
  9. import com.zhy.csdn.DataUtil;  
  10.   
  11. public class Test  
  12. {  
  13.   
  14.     @org.junit.Test  
  15.     public void test01()  
  16.     {  
  17.         NewsItemBiz biz = new NewsItemBiz();  
  18.         int currentPage = 1;  
  19.         try  
  20.         {  
  21.             /** 
  22.              * 业界 
  23.              */  
  24.             List<NewsItem> newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YEJIE, currentPage);  
  25.             for (NewsItem item : newsItems)  
  26.             {  
  27.                 System.out.println(item);  
  28.             }  
  29.   
  30.             System.out.println("----------------------");  
  31.             /** 
  32.              * 程序员杂志 
  33.              */  
  34.             newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_CHENGXUYUAN, currentPage);  
  35.             for (NewsItem item : newsItems)  
  36.             {  
  37.                 System.out.println(item);  
  38.             }  
  39.             System.out.println("----------------------");  
  40.             /** 
  41.              * 研发 
  42.              */  
  43.             newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YANFA, currentPage);  
  44.             for (NewsItem item : newsItems)  
  45.             {  
  46.                 System.out.println(item);  
  47.             }  
  48.             System.out.println("----------------------");  
  49.             /** 
  50.              * 移动 
  51.              */  
  52.             newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YIDONG, currentPage);  
  53.             for (NewsItem item : newsItems)  
  54.             {  
  55.                 System.out.println(item);  
  56.             }  
  57.             System.out.println("----------------------");  
  58.   
  59.         } catch (CommonException e)  
  60.         {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64.   
  65. }  

结果:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. NewsItem [id=0title=如何做到每天写代码?, date=2014-04-11 11:26, newsType=1]  
  2. NewsItem [id=0title=一周消息树:超级充电器来袭,30秒可为手机充满电, date=2014-04-11 15:20, newsType=1]  
  3. NewsItem [id=0title=Google Glass于4月15日在美对外开放购买,售价为1500美元, date=2014-04-11 08:01, newsType=1]  
  4. NewsItem [id=0title=Cortana与Siri、Google Now的较量:支持功能更多, date=2014-04-10 16:30, newsType=1]  
  5. NewsItem [id=0title=优秀Unix管理员的七个习惯, date=2014-04-10 10:58, newsType=1]  
  6. NewsItem [id=0title=国外用户也不幸福!Facebook强制用户必须下载Messager, date=2014-04-10 09:10, newsType=1]  
  7. NewsItem [id=0title=ThoughtWorks CTO谈IT职场女性:你并不奇怪, date=2014-04-09 18:18, newsType=1]  
  8. NewsItem [id=0title=微软转型之路:从Build 2014开始, date=2014-04-09 17:05, newsType=1]  
  9. NewsItem [id=0title=设计师为什么要学编程,开发者为什么要学设计?, date=2014-04-09 14:07, newsType=1]  
  10. NewsItem [id=0title=Windows 8.1 Update 1的下载地址和八点???知, date=2014-04-09 08:38, newsType=1]  
  11. ----------------------  
  12. NewsItem [id=0title=页面仔和他的小创新, date=2014-04-11 11:09, newsType=4]  
  13. NewsItem [id=0title=未来两年必须掌握的移动互联网技术与能力, date=2014-04-10 14:43, newsType=4]  
  14. NewsItem [id=0title=互联网思维到底是什么——移动浪潮下的新商业逻辑, date=2014-04-09 13:05, newsType=4]  
  15. NewsItem [id=0title=虚拟现实之眼——Oculus与HMD关键技术, date=2014-04-09 12:47, newsType=4]  
  16. NewsItem [id=0title=如何实现团队的自组织管理, date=2014-04-09 11:59, newsType=4]  
  17. NewsItem [id=0title=途牛网CTO汤峥嵘:互联网思维——光说不练远远不够, date=2014-04-08 11:10, newsType=4]  
  18. NewsItem [id=0title=理解创客, date=2014-04-04 17:55, newsType=4]  
  19. NewsItem [id=0title=TypeScript:更好的JavaScript, date=2014-04-03 16:10, newsType=4]  
  20. NewsItem [id=0title=Chris Anderson:我们正经历一场真正的革命, date=2014-04-02 14:45, newsType=4]  
  21. NewsItem [id=0title=Cocos2d-x 3.0带来了什么, date=2014-04-02 14:09, newsType=4]  
  22. ----------------------  
  23. NewsItem [id=0title=研发周报:Perl创历史新低, date=2014-04-11 14:13, newsType=3]  
  24. NewsItem [id=0title=代码面试最常用的10大算法, date=2014-04-10 11:34, newsType=3]  
  25. NewsItem [id=0title=TIOBE 2014年4月编程语言排行榜:Perl跌至历史最低点, date=2014-04-10 09:20, newsType=3]  
  26. NewsItem [id=0title=金蝶发布Apusic智慧云平台 构建产业联盟推动信息化建设, date=2014-04-09 10:38, newsType=3]  
  27. NewsItem [id=0title=OpenSSL究竟为何物,为何它的影响力如此之大?, date=2014-04-09 08:52, newsType=3]  
  28. NewsItem [id=0title=Airbnb的管理之道:产品设计的点评策略与技巧, date=2014-04-09 07:01, newsType=3]  
  29. NewsItem [id=0title=大势所趋 HTML5成Web开发者最关心的技术, date=2014-04-08 14:30, newsType=3]  
  30. NewsItem [id=0title=研发周报:微软Build2014精华汇总, date=2014-04-04 16:09, newsType=3]  
  31. NewsItem [id=0title=Facebook发布PlanOut 开源部分A/B测试源码, date=2014-04-04 11:02, newsType=3]  
  32. NewsItem [id=0title=撼动企业应用架构的十大技术趋势, date=2014-04-08 14:40, newsType=3]  
  33. ----------------------  
  34. NewsItem [id=0title=2014移动开发者必备的十大应用测试工具, date=22小时前, newsType=2]  
  35. NewsItem [id=0title=前《连线》主编Chris Anderson:创客就要DIT, date=22小时前, newsType=2]  
  36. NewsItem [id=0title=创客天下——《Make》及Maker Faire创办人、O'Reilly Media创始人Dale Dougherty专访, date=2014-04-11 11:21, newsType=2]  
  37. NewsItem [id=0title=《近匠》aGlass团队:透析眼控技术的价值, date=2014-04-11 10:51, newsType=2]  
  38. NewsItem [id=0title=UC多屏战略 推出电脑版和电视版浏览器, date=2014-04-11 07:07, newsType=2]  
  39. NewsItem [id=0title=“颠覆医疗” 时云医疗推三款硬件产品, date=2014-04-10 21:05, newsType=2]  
  40. NewsItem [id=0title=2014Unity亚洲开发者大会倒计时 干货内容日程汇总, date=2014-04-10 10:06, newsType=2]  
  41. NewsItem [id=0title=《近匠》棱镜:手游渠道SDK平台的技术历程, date=2014-04-09 10:27, newsType=2]  
  42. NewsItem [id=0title=绝对的超现实!Jaunt打造360°全景VR电影, date=2014-04-08 15:45, newsType=2]  
  43. NewsItem [id=0title=Unite China 2014课程解析:行业解决方案专场免费开放, date=2014-04-08 13:13, newsType=2]  
  44. ----------------------  

好了,最后打成jar,在下篇博客中会放入咱们待完成Android的项目中使用。



如果你觉得这篇文章对你有帮助,可以顶一个。


源码点击下载

0 0