`
jgsj
  • 浏览: 1051117 次
文章分类
社区版块
存档分类
最新评论

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



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


源码点击下载


分享到:
评论

相关推荐

    app数据抓取

    移动app抓取数据,进行数据防抓取防护,同时可以有效地分析抓取数据

    electron爬虫:定制CSDN App

    在这里,开发者可能通过爬虫技术来抓取CSDN网站上的数据,如博客文章、用户信息等,然后在Electron应用中展示。 描述中的链接指向了一个CSDN博客文章,文章详细介绍了如何进行这项工作。我们无法直接查看文章内容,...

    快手app短视频数据提取和保存

    在IT行业中,数据提取与保存是一项基础且重要的技能,尤其在大数据时代,对社交媒体平台如快手APP的数据进行分析,可以洞察用户行为、市场趋势等关键信息。本篇将详细讲解如何使用Python爬虫技术来提取并保存快手APP...

    获取CSDN文章内容并转换为markdown文本的python

    本篇介绍的是一款小型工具软件,其功能是能够直接抓取CSDN平台上的文章内容,并将其转换成Markdown格式的文本文件。该工具不仅方便用户在离线状态下阅读CSDN的文章,而且对于那些喜欢将资料整理成Markdown格式的人来...

    人立方(仿照CSDN的制作的)需要Tomcat服务器

    具体来说,它能够抓取CSDN网站上用户的好友列表以及最近的访客记录,帮助用户更好地理解和管理自己的网络人脉。 在实现人立方的过程中,关键的技术依赖是Apache Tomcat服务器。Tomcat是一款开源的Java Servlet容器...

    基于Python的爬取今日头条文章及视频+源代码+文档说明

    3. 根据文章url等信息,抓取文章内容 4. 若文章中包含视频,则取视频url,然后下载 具体实现 ------ ### 一、抓包 ### 1. 工具:`Fiddler`、 `android`手机、`google浏览器` 2. 步骤: (1)本示例采用[Fiddler...

    手机网站app

    在IT行业中,手机网站App开发是一项重要的技术领域,它涵盖了移动设备上的网页浏览、数据交互以及网络通信等多方面知识。本套资料主要关注的是如何使用App进行HTTP截包,以便于开发者理解和调试网络请求。以下是对...

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

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

    爬虫实例:微博、b站、csdn、淘宝、今日头条、知乎、豆瓣、知乎APP、大众点评.zip

    本压缩包文件“爬虫实例:微博、b站、csdn、淘宝、今日头条、知乎、豆瓣、知乎APP、大众点评.zip”包含了多个具体的爬虫项目,涉及到了中国主要的社交媒体平台、电子商务网站、新闻聚合平台以及本地生活服务应用。...

    无损音乐制作软件

    无损音乐制作软件是专为音乐爱好者和专业音频工程师设计的工具,它们旨在提供高质量的音频处理,确保在编辑和转换过程中保留原始音频文件的完整性。这些软件支持各种无损音频格式,如APE、FLAC等,以确保音质不受...

    Qualcomm平台开发如何抓取Log.

    #### 二、SMEM Log抓取 SMEM(SMall MEMory) Log是一种特殊的日志类型,通常包含了关于内存分配的信息。在Qualcomm平台中,有两种主要的方法来抓取SMEM Log: 1. **使用Trace32**: - 如果拥有Trace32工具,可以在...

    小红书抓取,微信小程序,抓包工具.zip

    在当前的互联网数据抓取与分析领域,小红书作为一个集社区分享和电商功能于一身的平台,拥有庞大的用户生成内容和商品数据。这些内容不仅是品牌营销的重要渠道,也是消费者获取信息的重要来源。因此,掌握小红书数据...

    getcsdnlistview:【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取

    在本项目中,"getcsdnlistview"是一个Android应用的开发实例,专注于从CSDN(China Software Developer Network)博客平台抓取首页的文章列表内容。这个应用的主要目的是展示如何在Android环境中实现网页数据的抓取...

    Android Studio设计APP实现与51单片机通过WIFI模块(ESP8266-01S)通讯控制LED灯亮灭的设计APP

    源码下载: ...详细内容也可看我的文章:https://mp.csdn.net/mp_blog/creation/editor/124478948《Android Studio设计APP实现与51单片机通过WIFI模块(ESP8266-01S)通讯控制LED灯亮灭的设计源码【详解】》

    电影资讯android app

    【电影资讯Android App开发详解】 在移动互联网时代,电影资讯类Android应用已成为用户获取影视信息、分享观影体验的重要平台。本项目以“电影资讯”为主题,通过从豆瓣API抓取数据,为用户提供实时的电影上映信息...

    Xposed+JustTrustMe.zip

    在实际操作中,你可以参考提供的链接(https://blog.csdn.net/qq_41901122/article/details/112847053)进行详细的安装和使用教程,以确保正确无误地完成整个过程。通过深入学习和实践,你将能够更好地理解和掌握这...

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

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

    河北空气质量实时发布系统APP

    【描述】在CSDN博客上的一篇文章中,作者分享了如何从零开始构建这样一个客户端应用的详细过程。这个过程涵盖了Android应用开发的基础知识,包括: 1. **项目初始化**:使用Android Studio创建新项目,选择合适的...

    苹果手机应用信息抓取

    应用的appleID是其在App Store上的唯一标识,通常在应用的URL中可以找到,例如`id1234567890`。在iOS中,我们无法直接获取这个信息,因为它是私有的。但可以通过App Store Connect API或者第三方服务间接获取,这...

    USBlyzer(USB数据采集软件)

    USB数据采集软件。 使用此工具,我们尝试捕获以下内容 与控制器关联的数据: USB 供应商 ID 和产品 ID HID 枚举(用于 HID 设备,这可能显示用于多点触控的 USB 设置功能请求...) 触摸数据包 ...

Global site tag (gtag.js) - Google Analytics