抓取csdn上的各类别的文章 (制作csdn app 二)
转载请表明出处: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
package com.zhy.bean;public class NewsItem{ private int id; /** * 标题 */ private String title; /** * 链接 */ private String link; /** * 发布日期 */ private String date; /** * 图片的链接 */ private String imgLink; /** * 内容 */ private String content; /** * 类型 * */ private int newsType; public int getNewsType() { return newsType; } public void setNewsType(int newsType) { this.newsType = newsType; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getImgLink() { return imgLink; } public void setImgLink(String imgLink) { this.imgLink = imgLink; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "NewsItem [id=" + id + ", title=" + title + ", link=" + link + ", date=" + date + ", imgLink=" + imgLink + ", content=" + content + ", newsType=" + newsType + "]"; }}
CommonException.java
package com.zhy.bean;public class CommonException extends Exception{ public CommonException() { super(); // TODO Auto-generated constructor stub } public CommonException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public CommonException(String message) { super(message); // TODO Auto-generated constructor stub } public CommonException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
Constaint.java
package com.zhy.csdn;public interface Constaint{ public static final int NEWS_TYPE_YEJIE = 1; public static final int NEWS_TYPE_YIDONG = 2; public static final int NEWS_TYPE_YANFA = 3; public static final int NEWS_TYPE_CHENGXUYUAN = 4; public static final int NEWS_TYPE_YUNJISUAN = 5; }
DataUtil.java
package com.zhy.csdn;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import com.zhy.bean.CommonException;public class DataUtil{ /** * 返回该链接地址的html数据 * * @param urlStr * @return * @throws CommonException */ public static String doGet(String urlStr) throws CommonException { StringBuffer sb = new StringBuffer(); try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setDoInput(true); conn.setDoOutput(true); if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len, "UTF-8")); } is.close(); } else { throw new CommonException("访问网络失败!"); } } catch (Exception e) { throw new CommonException("访问网络失败!"); } return sb.toString(); } }
URLUtil.java
package com.zhy.csdn;public class URLUtil{ public static final String NEWS_LIST_URL = "http://www.csdn.net/headlines.html"; public static final String NEWS_LIST_URL_YIDONG = "http://mobile.csdn.net/mobile"; public static final String NEWS_LIST_URL_YANFA = "http://sd.csdn.net/sd"; public static final String NEWS_LIST_URL_YUNJISUAN = "http://cloud.csdn.net/cloud"; public static final String NEWS_LIST_URL_ZAZHI = "http://programmer.csdn.net/programmer"; public static final String NEWS_LIST_URL_YEJIE = "http://news.csdn.net/news"; /** * 根据文章类型,和当前页码生成url * @param newsType * @param currentPage * @return */ public static String generateUrl(int newsType, int currentPage) { currentPage = currentPage > 0 ? currentPage : 1; String urlStr = ""; switch (newsType) { case Constaint.NEWS_TYPE_YEJIE: urlStr = NEWS_LIST_URL_YEJIE; break; case Constaint.NEWS_TYPE_YANFA: urlStr = NEWS_LIST_URL_YANFA; break; case Constaint.NEWS_TYPE_CHENGXUYUAN: urlStr = NEWS_LIST_URL_ZAZHI; break; case Constaint.NEWS_TYPE_YUNJISUAN: urlStr = NEWS_LIST_URL_YUNJISUAN; break; default: urlStr = NEWS_LIST_URL_YIDONG; break; } urlStr += "/" + currentPage; return urlStr; }}
NewsItemBiz.java业务类
package com.zhy.biz;import java.util.ArrayList;import java.util.List;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;import com.zhy.bean.CommonException;import com.zhy.bean.NewsItem;import com.zhy.csdn.DataUtil;import com.zhy.csdn.URLUtil;/** * 处理NewItem的业务类 * @author zhy * */public class NewsItemBiz{ /** * 业界、移动、云计算 * * @param htmlStr * @return * @throws CommonException */ public List<NewsItem> getNewsItems( int newsType , int currentPage) throws CommonException { String urlStr = URLUtil.generateUrl(newsType, currentPage); String htmlStr = DataUtil.doGet(urlStr); List<NewsItem> newsItems = new ArrayList<NewsItem>(); NewsItem newsItem = null; Document doc = Jsoup.parse(htmlStr); Elements units = doc.getElementsByClass("unit"); for (int i = 0; i < units.size(); i++) { newsItem = new NewsItem(); newsItem.setNewsType(newsType); Element unit_ele = units.get(i); Element h1_ele = unit_ele.getElementsByTag("h1").get(0); Element h1_a_ele = h1_ele.child(0); String title = h1_a_ele.text(); String href = h1_a_ele.attr("href"); newsItem.setLink(href); newsItem.setTitle(title); Element h4_ele = unit_ele.getElementsByTag("h4").get(0); Element ago_ele = h4_ele.getElementsByClass("ago").get(0); String date = ago_ele.text(); newsItem.setDate(date); Element dl_ele = unit_ele.getElementsByTag("dl").get(0);// dl Element dt_ele = dl_ele.child(0);// dt try {// 可能没有图片 Element img_ele = dt_ele.child(0); String imgLink = img_ele.child(0).attr("src"); newsItem.setImgLink(imgLink); } catch (IndexOutOfBoundsException e) { } Element content_ele = dl_ele.child(1);// dd String content = content_ele.text(); newsItem.setContent(content); newsItems.add(newsItem); } return newsItems; }}
好了,最后就是测试了,这里使用单元测试,下面是测试代码和结果。
测试代码:
package com.zhy.test;import java.util.List;import com.zhy.bean.CommonException;import com.zhy.bean.NewsItem;import com.zhy.biz.NewsItemBiz;import com.zhy.csdn.Constaint;import com.zhy.csdn.DataUtil;public class Test{ @org.junit.Test public void test01() { NewsItemBiz biz = new NewsItemBiz(); int currentPage = 1; try { /** * 业界 */ List<NewsItem> newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YEJIE, currentPage); for (NewsItem item : newsItems) { System.out.println(item); } System.out.println("----------------------"); /** * 程序员杂志 */ newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_CHENGXUYUAN, currentPage); for (NewsItem item : newsItems) { System.out.println(item); } System.out.println("----------------------"); /** * 研发 */ newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YANFA, currentPage); for (NewsItem item : newsItems) { System.out.println(item); } System.out.println("----------------------"); /** * 移动 */ newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YIDONG, currentPage); for (NewsItem item : newsItems) { System.out.println(item); } System.out.println("----------------------"); } catch (CommonException e) { e.printStackTrace(); } }}
结果:
NewsItem [id=0, title=如何做到每天写代码?, date=2014-04-11 11:26, newsType=1]NewsItem [id=0, title=一周消息树:超级充电器来袭,30秒可为手机充满电, date=2014-04-11 15:20, newsType=1]NewsItem [id=0, title=Google Glass于4月15日在美对外开放购买,售价为1500美元, date=2014-04-11 08:01, newsType=1]NewsItem [id=0, title=Cortana与Siri、Google Now的较量:支持功能更多, date=2014-04-10 16:30, newsType=1]NewsItem [id=0, title=优秀Unix管理员的七个习惯, date=2014-04-10 10:58, newsType=1]NewsItem [id=0, title=国外用户也不幸福!Facebook强制用户必须下载Messager, date=2014-04-10 09:10, newsType=1]NewsItem [id=0, title=ThoughtWorks CTO谈IT职场女性:你并不奇怪, date=2014-04-09 18:18, newsType=1]NewsItem [id=0, title=微软转型之路:从Build 2014开始, date=2014-04-09 17:05, newsType=1]NewsItem [id=0, title=设计师为什么要学编程,开发者为什么要学设计?, date=2014-04-09 14:07, newsType=1]NewsItem [id=0, title=Windows 8.1 Update 1的下载地址和八点???知, date=2014-04-09 08:38, newsType=1]----------------------NewsItem [id=0, title=页面仔和他的小创新, date=2014-04-11 11:09, newsType=4]NewsItem [id=0, title=未来两年必须掌握的移动互联网技术与能力, date=2014-04-10 14:43, newsType=4]NewsItem [id=0, title=互联网思维到底是什么——移动浪潮下的新商业逻辑, date=2014-04-09 13:05, newsType=4]NewsItem [id=0, title=虚拟现实之眼——Oculus与HMD关键技术, date=2014-04-09 12:47, newsType=4]NewsItem [id=0, title=如何实现团队的自组织管理, date=2014-04-09 11:59, newsType=4]NewsItem [id=0, title=途牛网CTO汤峥嵘:互联网思维——光说不练远远不够, date=2014-04-08 11:10, newsType=4]NewsItem [id=0, title=理解创客, date=2014-04-04 17:55, newsType=4]NewsItem [id=0, title=TypeScript:更好的JavaScript, date=2014-04-03 16:10, newsType=4]NewsItem [id=0, title=Chris Anderson:我们正经历一场真正的革命, date=2014-04-02 14:45, newsType=4]NewsItem [id=0, title=Cocos2d-x 3.0带来了什么, date=2014-04-02 14:09, newsType=4]----------------------NewsItem [id=0, title=研发周报:Perl创历史新低, date=2014-04-11 14:13, newsType=3]NewsItem [id=0, title=代码面试最常用的10大算法, date=2014-04-10 11:34, newsType=3]NewsItem [id=0, title=TIOBE 2014年4月编程语言排行榜:Perl跌至历史最低点, date=2014-04-10 09:20, newsType=3]NewsItem [id=0, title=金蝶发布Apusic智慧云平台 构建产业联盟推动信息化建设, date=2014-04-09 10:38, newsType=3]NewsItem [id=0, title=OpenSSL究竟为何物,为何它的影响力如此之大?, date=2014-04-09 08:52, newsType=3]NewsItem [id=0, title=Airbnb的管理之道:产品设计的点评策略与技巧, date=2014-04-09 07:01, newsType=3]NewsItem [id=0, title=大势所趋 HTML5成Web开发者最关心的技术, date=2014-04-08 14:30, newsType=3]NewsItem [id=0, title=研发周报:微软Build2014精华汇总, date=2014-04-04 16:09, newsType=3]NewsItem [id=0, title=Facebook发布PlanOut 开源部分A/B测试源码, date=2014-04-04 11:02, newsType=3]NewsItem [id=0, title=撼动企业应用架构的十大技术趋势, date=2014-04-08 14:40, newsType=3]----------------------NewsItem [id=0, title=2014移动开发者必备的十大应用测试工具, date=22小时前, newsType=2]NewsItem [id=0, title=前《连线》主编Chris Anderson:创客就要DIT, date=22小时前, newsType=2]NewsItem [id=0, title=创客天下——《Make》及Maker Faire创办人、O'Reilly Media创始人Dale Dougherty专访, date=2014-04-11 11:21, newsType=2]NewsItem [id=0, title=《近匠》aGlass团队:透析眼控技术的价值, date=2014-04-11 10:51, newsType=2]NewsItem [id=0, title=UC多屏战略 推出电脑版和电视版浏览器, date=2014-04-11 07:07, newsType=2]NewsItem [id=0, title=“颠覆医疗” 时云医疗推三款硬件产品, date=2014-04-10 21:05, newsType=2]NewsItem [id=0, title=2014Unity亚洲开发者大会倒计时 干货内容日程汇总, date=2014-04-10 10:06, newsType=2]NewsItem [id=0, title=《近匠》棱镜:手游渠道SDK平台的技术历程, date=2014-04-09 10:27, newsType=2]NewsItem [id=0, title=绝对的超现实!Jaunt打造360°全景VR电影, date=2014-04-08 15:45, newsType=2]NewsItem [id=0, title=Unite China 2014课程解析:行业解决方案专场免费开放, date=2014-04-08 13:13, newsType=2]----------------------
好了,最后打成jar,在下篇博客中会放入咱们待完成Android的项目中使用。
如果你觉得这篇文章对你有帮助,可以顶一个。
源码点击下载
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
抓取csdn上的各类别的文章 (制作csdn app 二)相关推荐
- php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急
PHP抓取页面上的数组 并循环输出 急 在线等 我用file_get_contents()抓取了 这个网址上的内容 http://simonfenci.sinaapp.com/index.php?ke ...
- 用python画爱心动图_编写Python爬虫抓取暴走漫画上gif图片的实例分享
本文要介绍的爬虫是抓取暴走漫画上的GIF趣图,方便离线观看.爬虫用的是python3.3开发的,主要用到了urllib.request和BeautifulSoup模块. urllib模块提供了从万维网 ...
- gif提取 python_编写Python爬虫抓取暴走漫画上gif图片的实例分享
本文要介绍的爬虫是抓取暴走漫画上的GIF趣图,方便离线观看.爬虫用的是python3.3开发的,主要用到了urllib.request和BeautifulSoup模块. urllib模块提供了从万维网 ...
- Postman设置代理抓取手机上的请求
一般为方便接口测试,我们都会设置代理,抓取手机上的请求来方便测试,具体的操作方法如下. 操作步骤: 一.手机和电脑连接同一网络,查看电脑连接网络的IP,配置手机的代理 1.查看电脑连接的网络与IP地址 ...
- 拼多多上货助手如何批量抓取商品上传?
拼多多上传商品方面要注意什么呢?很多商家在上传商品时没有刻意的注意商品的sku.标题等设立,导致商品在后期受到平台的处罚.下架.权重降低等情况的出现,是什么原因导致商品在后期受到处罚呢?一方面是商品的 ...
- 京东上货工具哪个好?怎么抓取商品上传?
京东平台,相信很多商家都知道这个平台了,京东的商家很多都会在其他平台开店,所以京东商家在上货方面就比较简单多了.直接在其他平台抓取商品复制到京东就可以了.如何抓取淘宝商品上传到京东呢? ...
- 抓取网页上的图片(一)
抓取网页上的图片 思路:网页(HTML)中的图片通常在img标签中,图片的链接通常在标签的src属性中,通过BeautifulSoup解析HTML,找到所有的img标签,获取每一个标签中的src的属性 ...
- python抓取网站图片_python抓取图片示例 python抓取网页上图片
python抓取网页上图片 这个错误时是什么意思 下面是代码 import re import urllib.request imp正则表达式匹配的url有错误 for x in add: print ...
- 用curl+PHP抓取网页上所需要的数据
最近使用后台的时候老是发现有些信息没法导出,又不想一个个复制很麻烦,想起以前做的一个小功能,用来抓取网页上的可用数据填充自己的数据库,由于网站需要登录才能访问,所以加上了curl的模拟登录 <? ...
- qcy教你下载你在CSDN上写过的文章
前言 现在是2017年4月27日0:20:38. 异常兴奋- 事情是这样的. 因为在高中(2009年)开运动会的时候,我们录了好多像.但是后来我重装系统的时候,我自己确实是个电脑盲(尽管那个时候还是个 ...
最新文章
- mongoDB设置用户名密码的一个要点
- linux ubuntu下怎样将pdf格式文件转换为doc格式文件,Ubuntu环境下把word文档转成pdf,把pdf文件转成jpg...
- POS主密钥与工作密钥关联详解
- linux 命令(3)echo
- 以GIS面对崛起的城市群
- 台式电源GX450的开关O和-到底什么意思
- python 阿狸的进阶之路(4)
- 工厂设计模式案例研究
- docker安装mysql5.6,安装redis3.2
- sql查看表的数据大小_查看Oracle 数据库的每天归档量及数据库大小
- 通过demo搞懂encode_utf8和decode_utf8
- 爬虫学习之-Python list 和 str 互转
- SSH框架总结(框架分析+环境搭建+实例源码下载)
- (转)Managed DirectX +C# 开发(入门篇)(二)
- 字典重复key的合并
- 谷歌SEO是什么意思,谷歌搜索引擎优化怎么做
- 2015年维多利亚的秘密新晋十位天使
- img图片加载失败的处理
- (bat)检查今天星期几,并执行程序
- 论今日头条背后的产品逻辑分析