`
king_tt
  • 浏览: 2115215 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

抓取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的项目中使用。



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


源码点击下载


分享到:
评论

相关推荐

    电影资讯android app

    简单的安卓应用 从豆瓣抓取数据 有正在上映 即将上映 排行榜等。以及想看、已看、分享、搜索等功能

    APP抓包( 过二次校验思路)

    过双向校验的,得先去APP里面找到证书,然后导入到抓包工具里面,因为在向前面所述博客文章,去伪造证书已经不管用了,我们先去找找特征。 比如 搜索 运气好的话能搜索到证书,但是一般都有密码。。 我这里这个APP...

    网探网页数据监控软件 v1.2.16.12.zip

    网探网页数据监控软件现在各行各业都在应用互联网技术,网上的数据也越来越丰富。有些数据的价值是与时间相关的,早一点得知就会很有用,晚了可能价值已经归零。网探这个软件就是来解决这一类问题的,让你“时时先人...

    Fiddler抓包工具

    Fiddler抓包工具,用于抓取app发起请求Fiddler抓包工具,用于抓取app发起请求Fiddler抓包工具,用于抓取app发起请求

    Mac Charles

    在Mac用Charles抓取移动端app的网络请求,破解版压缩包。

    nutritional-app:营养App React js

    营养App食品 :stuffed_flatbread: :leafy_green: -React JS 开发要求 功能组件 使用道具 连接到API 分段 三元运算符和querySelector 使用Material-UI并使用其组件 useState 使用抓取 营养api后端(Recipe Api)的...

    随身调测平台GT.zip

    GT(随身调)是APP的随身调测平台,它是直接运行在手机上的“集成调测环境”(IDTE, Integrated Debug Environment)。利用GT,仅凭一部手机,无需连接电脑,您即可对APP进行快速的性能测试(CPU、内存、流量、电量、...

    基于android的网上商城

    商品的推荐、浏览,改写上拉刷新下拉加载加载商品信息,商品信息是通过Jsoup从网页上抓取,获得XML解析并封装成实体类,然后适配到adapter;当然用户也可以搜索感兴趣的商品和店铺,Jsoup发送post请求后得到用户搜索...

    支付宝到账DEMO.exe

    具体可以看我的博客文章 https://blog.csdn.net/zyqytsoft/article/details/107161356 由于支付宝签约接口有千6的手续费 网上查了一下 支付宝免签约即时到帐的实现方案 基本都是在转账备注里按格式写入固定格式数据 ...

    2020中国互联网发展趋势报告-Fastdata极数-202003 (3).pdf

    端数据:针对特定类型平台进行不同维度及口径的数据抓取、数据结构化处理、存储及统计分析。 宏观数据:来源渠道主要包括 Wind 、 choice 、彭博、各国相关统计机构、国际组织、第三方数据机构等。 统计周期:...

    Edxposed框架涉及到的安装包

    Fiddler利用Edxposed框架+TrustMeAlready来突破SSL pinning抓取手机APP数据 https://blog.csdn.net/u014644574/article/details/127995031

    Android代码-RxJava Retrofit MVP精仿今日头条

    精仿今日头条,数据是抓取今日头条App的数据。使用RxJava Retrofit MVP开发的开源项目,仅供学习用途。觉得对你有帮助的话请帮忙star一下,让更多人知道,多谢啦! 感谢大神 wey ye,项目中参考到他做过的仿今日头条...

    网格世界

    如果在Windows上运行,请确保获得Build在Mac上运行,请确保获得Build.app 。 如果您在Mac上运行并且遇到问题,请尝试使该文件不是可执行文件: cd Build.app/Contents/MacOS & chmod +x GridWorld 。 确保播放Grid

    flutter.pftrace

    测试flutter还是之前写的https://blog.csdn.net/sinat_20059415/article/details/105920254#t4里的app 用perfetto抓取的trace 抓的不大对,没抓到布局相关trace

    微信小程序抓包

    我们知道,微信小程序的请求接口都是HTTPS,因此单纯的使用Burpsuite无法抓取数据包,原因是APP启用了SSL Pinning(又叫做“SSL证书绑定”)。 至于原理就不过多的进行介绍。 首先,分享一下我整理的所需要的安装包:...

    C#信息采集工具实现

    3.如果抓取的页面很多 ,可以把多线程跟队列应用过来,提高抓取效率 Queue&lt;int&gt; numbers = new Queue(); const int MaxCount = 5;//同时运行的最多线程数 private static object _lock = new object(); ...

    pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z

    1 表示 i2c-1这条I2C1总线上挂载的设备,如果是I2C2总线上挂载的设备,路径就是2-00XX了。 0051 一般的I2C设备的从机地址都是一个字节的,因为前两位为0x00(16进制的),后两位为pcf8563移位自后的I2C从机地址0x51...

Global site tag (gtag.js) - Google Analytics