Android Jsoup 爬取网页数据
2017-03-20 16:38
204 查看
一不小心一个月又过去了,其实最近还是小忙小忙的,废话不多说,直接进入今天的主题吧。
Jsoup – Java HTML Parser, with best of DOM, CSS, and jquery.,看这个介绍就知道,这个就是方便咱们
那看到最后的
来吧,加入我们有下面一段html标签需要解析:
比如我们要在茫茫标签中找到
![](http://upload-images.jianshu.io/upload_images/2244299-8d3a2322822448bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
哈哈,很easy嘛,那就是
这里得到是一个集合。我们接下来就需要遍历这个集合,然后把里面的每一个标签都拔出来。
title 部分的解析,这里是一个
这里又多了
这里还是要回到
![](http://upload-images.jianshu.io/upload_images/2244299-11e8330590954d6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
对于
代码就是这样的,这里一不小心就又引出了
![](http://upload-images.jianshu.io/upload_images/2244299-3289ffbe43ff9399.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其实不用太多解释啦,截图里面描述的很清楚了。最后一个是可以支持正则的匹配。
这里我们只需要解析到第二个
![](http://upload-images.jianshu.io/upload_images/2244299-a9406e8f13ceda3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
有时候我们只知道这个
在
就是
所以灵机一动,为什么我不自己搞一个
最后来一波效果图:
项目地址:https://github.com/lovejjfg/Android-Arsenal
![](http://upload-images.jianshu.io/upload_images/2244299-687e062302d62b25.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-ee4739726baac695.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-ae10c8425b4b6e8b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-6af2de39c8a61312.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-f57e7ad56c1400f8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-f56ef2efb47fc45c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
—- Edit By Joe At 2017 03 18 —-
Jsoup – Java HTML Parser, with best of DOM, CSS, and jquery.,看这个介绍就知道,这个就是方便咱们
Java和
Android来解析
HTML的。
HTML 标签
要去爬别人的HTML标签的话,首先你肯定得有一定的
HTML的基础知识吧。比如说常用的标签,标签的相关属性,这个就不多说了,有相关问题都可以在 www.w3school.com.cn 的网站解决一下。
加载网页
最简单的,直接加载一个网页:Document document = Jsoup.connect("https://www.google.com").get();
那看到最后的
get()方法聪明滴你一定就猜到还有一个对应的
post()方法了吧。另外,
http请求的相关操作都是可以设置的,包括
header请求参数,请求超时等等。除此之外,本地的文件(IO流)等都是可以直接解析的哈。
Document document = Jsoup.connect("https://android-arsenal.com") .timeout(5000) .cookie("cookie", "cxxx") .header("xx", "xx") .userAgent("") .get();
基本标签解析
之后咱们就得到了一个Document的对象了。这个对象就是对整个请求网页的封装,相关内容都可以在里面获取。
来吧,加入我们有下面一段html标签需要解析:
RendererRecyclerViewAdapter Recycler Views Free New A single adapter for the whole project. Now you do not need to implement adapters for RecyclerView. You can easily use several types of cells in a single list. Using this library will protect you from the appearance of any business logic in an adapter. [/i] Mar 17, 2017 " data-snippet-id="ext.8586638da0117b735369be55f34e6d37" data-snippet-saved="false" data-codota-status="done">[code]<div class="project-info clearfix"> <div class="header"> <div class="title"> <a href="/details/1/5442">RendererRecyclerViewAdapter</a> <a class="tags" href="/tag/199">Recycler Views</a> </div> <a class="badge free" href="/free">Free</a> <a class="badge new" href="/recent">New</a> </div> <div class="desc"> <p>A single adapter for the whole project.</p> <ul> <li>Now you do not need to implement adapters for RecyclerView.</li> <li>You can easily use several types of cells in a single list.</li> <li>Using this library will protect you from the appearance of any business logic in an adapter.</li> </ul> </div> <div class="ftr l"><i class="fa fa-calendar"></i> Mar 17, 2017</div> </div>
Jsoup里面对于标签的寻找使用的方法是
select()方法,这个方法不要太强大了。咱们一步一步的来。
比如我们要在茫茫标签中找到
<div class="project-info clearfix">的话,拿这里就是应该
findElementByClass(),那么在
Jsoup中是怎么定义这一块的呢?
![](http://upload-images.jianshu.io/upload_images/2244299-8d3a2322822448bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
哈哈,很easy嘛,那就是
document.select("div.project-info clearfix")咯,当然不是这样子的,等等
class属性里面这个空格是什么意思啊?是不是一脸懵逼?这里最终的写法是
document.select("div.project-info.clearfix")空格需要用
.来处理。
Elements select = document.select("div.project-info.clearfix");
这里得到是一个集合。我们接下来就需要遍历这个集合,然后把里面的每一个标签都拔出来。
title 部分的解析,这里是一个
<div>里面嵌套了一个
<a>的标签。这里就涉及到了解析
<a>标签了。这里我们需要对应的
href,也需要对应的
text,
Jsoup提供了对应的两个方法
attr()和
text()。
Elements elements = e.select("div.title"); if (!elements.isEmpty()) { for (Element tittle : elements) { Element first = tittle.select("a[href]").first(); if (first != null) { title = first.text(); titleUrl = first.attr("href"); System.out.println("名称:" + title); System.out.println("具体地址:" + titleUrl); } Elements select1 = tittle.select("a.tags"); if (!select1.isEmpty()) { tag = select1.text(); tagUrl = select1.attr("href"); System.out.println("tags:" + tag); System.out.println("tagUrl:" + tagUrl); } } }
嵌套解析
到这里,<div>和
<a>标签的介绍基本搞定,接下来就是
<div class="desc">的解析了。
<div class="desc"> <p>A single adapter for the whole project.</p> <ul> <li>Now you do not need to implement adapters for RecyclerView.</li> <li>You can easily use several types of cells in a single list.</li> <li>Using this library will protect you from the appearance of any business logic in an adapter.</li> </ul> </div>
这里又多了
<ul>和
<li>了,其实道理是差不多的,但是这里它们既没有
class也没有
id,那这个我们应该这么去解析呢?
这里还是要回到
select()方法,这里就需要使用到指定层级的方法了。
![](http://upload-images.jianshu.io/upload_images/2244299-11e8330590954d6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Elements select1 = e.select("div.desc > p"); String s = select1.toString();
对于
<dt>
<dd>相关的标签,就可以使用
+相关的连接符了。例如我想要只解析
Tag下面的对应的
Tag名称和相关的
url,这个应该怎么写呢?
<dt>Tag</dt> <dd><a href="/tag/9">Background Processing</a></dd> <dt>Lice bfb9 nse</dt> <dd><a href="http://opensource.org/licenses/Apache-2.0" rel="nofollow" target="_blank">Apache License, Version 2.0</a> </dd>
代码就是这样的,这里一不小心就又引出了
select()方法的嵌套高级写法。
Elements select4 = element.select("dt:contains(Tag) + dd");
![](http://upload-images.jianshu.io/upload_images/2244299-3289ffbe43ff9399.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其实不用太多解释啦,截图里面描述的很清楚了。最后一个是可以支持正则的匹配。
同级相邻解析
还有一种情况就是我们需要的标签没有具体的id或者
class,并且它没有直接对应的父标签或者某种固定的嵌套关系,例如下面这种情况:
ImmediateLooperScheduler " data-snippet-id="ext.2bc5ebe9840f0e244465396f590a3cf3" data-snippet-saved="false" data-codota-status="done">[code]<a id="favoriteButton" href="#" class="fa fa-star-o favorite tshadow" title="Add to favorites"></a> <a href="/details/1/5244">ImmediateLooperScheduler</a> <div id="githubInfoValue">
这里我们只需要解析到第二个
<a>标签,那么需要怎么处理呢?这里就需要使用到
nextElementSibling()的方法了。
Element ssa = h1.select("a#favoriteButton").first(); Element element = ssa.nextElementSibling(); String title = element.text();
模糊解析
![](http://upload-images.jianshu.io/upload_images/2244299-a9406e8f13ceda3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
有时候我们只知道这个
<div>是以 什么开头或者是以什么结尾或者又是里面包含了某个单词的,那么这个时候就需要使用模糊查找了。
在
Jsoup中定义了这些情况的相关
select()写法,其中,以什么开头,是使用
a[href^=http],以什么结尾使用
a[href$=.jpg],包含什么就是使用
a[href*=/search/]。
javascript 解析
刚刚说的都是普通标签及其内容,如果我要获取js相关的标签以及内容呢?其实也不难,只是最后不是使用text()的方法,而是使用data()的方法了。就是
Jsoup最主要的就是写好这个
select()方法,
final Elements script = document.select("script"); String js = script.first().data();
相关实战
Android-Arsenal 这个网站不造大家伙儿有听说过没?这里给我们Android开发者提供了了一个信息交流展示平台,实时更新一些Android相关的App,开发库、以及Demo。然后,我看到它也有自己的客户端,所以一时好奇也打算下载下来看看,结果,客户端就是直接加载的网页,关键是广告满天飞。这个就让人不好受了(话说回来,人家不打广告赚点儿钱做这个平台干嘛呢。)所以灵机一动,为什么我不自己搞一个
Android-Arsenal的客户端呢?这样方便在手机上看到最新的东西嘛。所以就做了一个客户端,而使用的就是
Jsoup来爬去的对应的网页。然后就把对应
ads的标签都过滤了,所以是很清爽的啦。当然功能也只是先实现了一部分。喜欢的朋友可以点个星星或者下载使用哟!
最后来一波效果图:
项目地址:https://github.com/lovejjfg/Android-Arsenal
![](http://upload-images.jianshu.io/upload_images/2244299-687e062302d62b25.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-ee4739726baac695.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-ae10c8425b4b6e8b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-6af2de39c8a61312.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-f57e7ad56c1400f8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2244299-f56ef2efb47fc45c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
—- Edit By Joe At 2017 03 18 —-
相关文章推荐
- Android利用jsoup爬虫爬网页数据(一)
- Android Jsoup 爬取网页数据
- Android利用jsoup爬虫爬网页数据(二)
- jsoup抓取网页上的数据,Android同时获取数据
- Android中使用jsoup对网页数据的抓取和修改
- Android App使用Jsoup抓取网页数据显示
- Jsoup抓取网页数据完成一个简易的Android新闻APP
- android:获取富文本图片和使用Jsoup抓取腾讯新闻网页数据
- Jsoup抓取网页数据完成一个简易的Android新闻APP
- Android自由开发者 通过Jsoup抓取网页数据
- android中使用JSOUP如何解析网页数据详述
- Android WebView 获取网页数据(html)
- 使用Jsoup进行网页数据抓取
- android解压缩GZIP格式的网页数据
- Android开发_Jsoup抓取网页信息
- Android之使用Jsoup抓取网络数据
- android用jsoup分析网页
- Android中获取网页表单中的数据
- Android中获取网页表单中的数据
- jsoup技术抓取网页数据大全