发布于 2016-08-08 22:18:41 | 405 次阅读 | 评论: 0 | 来源: 网友投递
Java程序设计语言
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
一、需求
最近基于 Material Design 重构了自己的新闻 App,数据来源是个问题。
有前人分析了知乎日报、凤凰新闻等 API,根据相应的 URL 可以获取新闻的 JSON 数据。为了锻炼写代码能力,笔者打算爬虫新闻页面,自己获取数据构建 API。
二、效果图
下图是原网站的页面
爬虫获取了数据,展示到 APP 手机端
三、爬虫思路
关于App 的实现过程可以参看这几篇文章,本文主要讲解一下如何爬虫数据。
Android下录制App操作生成Gif动态图的全过程 :/article/16/0617/191580.html
学习Android Material Design(RecyclerView代替ListView):/article/16/0617/191582.html
Android项目实战之仿网易新闻的页面(RecyclerView ):/article/16/0228/191583.html
Jsoup 简介
Jsoup 是一个 Java 的开源HTML解析器,可直接解析某个URL地址、HTML文本内容。
Jsoup主要有以下功能:
四、爬虫过程
Get 请求获取网页 HTML
新闻网页Html的DOM树如下所示:
下面这段代码根据指定的 url,用代码获取get 请求返回的 html 源代码。
public static String doGet(String urlStr) throws CommonException {
URL url;
String html = "";
try {
url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.setDoOutput(true);
if (connection.getResponseCode() == 200) {
InputStream in = connection.getInputStream();
html = StreamTool.inToStringByByte(in);
} else {
throw new CommonException("新闻服务器返回值不为200");
}
} catch (Exception e) {
e.printStackTrace();
throw new CommonException("get请求失败");
}
return html;
}
InputStream in = connection.getInputStream();将得到输入流转化为字符串是个普遍需求,我们将其抽象出来,写一个工具方法。
public class StreamTool {
public static String inToStringByByte(InputStream in) throws Exception {
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
StringBuilder content = new StringBuilder();
while ((len = in.read(buffer)) != -1) {
content.append(new String(buffer, 0, len, "UTF-8"));
}
outStr.close();
return content.toString();
}
}
五、解析 HTML 获取标题
利用 google 浏览器的审查元素,找出新闻标题对于的html 代码:
<div id="article_title">
<h1>
<a href="http://see.xidian.edu.cn/html/news/7428.html">
关于举办《经典音乐作品欣赏与人文审美》讲座的通知
</a>
</h1>
</div>
我们需要从上面的 HTML 中找出id="article_title"的部分,使用 getElementById(String id) 方法
String htmlStr = HttpTool.doGet(urlStr);
// 将获取的网页 HTML 源代码转化为 Document
Document doc = Jsoup.parse(htmlStr);
Element articleEle = doc.getElementById("article");
// 标题
Element titleEle = articleEle.getElementById("article_title");
String titleStr = titleEle.text();
六、获取发布日期、信息来源
同样找出对于的 HTML 代码
<html>
<head></head>
<body>
<div id="article_detail">
<span> 2015-05-28 </span>
<span> 来源: </span>
<span> 浏览次数: <script language="JavaScript" src="http://img.phperz.com/data/img/20160804/1470317868_9585.png],
title=电院2014级开展“让诚信之花开遍冬日校园”教育活动,
publishDate=2016-01-14,
source=来源: 电影新闻网,
readTimes=200,
body=<div id="article_content">
<p style="text-indent:2em;" align="justify"> <strong><span style="font-size:16px;line-height:1.5;">西电新闻网讯</span></strong><span style="font-size:16px;line-height:1.5;"> (通讯员</span><strong><span style="font-size:16px;line-height:1.5;"> 丁彤 王朱丹</span></strong><span style="font-size:16px;line-height:1.5;">...)
本文讲解了如何实现Jsoup 网络爬虫,如果文章对您有帮助,那就给个赞吧。