原由:我在访问服务器时,回来的数据是html源文件,我需要从html源文件中获取对自己有用的信息。

一般情况下jsoup多用于JAVA方面的开发,可以利用jQuery,DOM一同使用。可以达到抓取其它网站的信息。

好了开始介绍如何使用jsoup在android中解析html的使用

1. 下载jsoup包 http://jsoup.org/download

jsoup官网:http://jsoup.org/

我这里下载的是jsoup-1.7.2.jar core library

2. 将下载下来的jar包放到放到Android工程中 libs目录下

在ADT稍微新一点的版本,放到libs下刷新一下,就可以用了

如果ADT比较老可能需要自己手动引入一下jar包(这个自己网上查一下)

3.在代码中使用

这个是一个中文开发手册
http://www.open-open.com/jsoup/ 大家可以学习一下。

简单附几句代码:

[java] view plain copy print?
  1. String html = "<html><head><title>First parse</title></head>"
  2. + "<body><p>Parsed HTML into a doc.</p></body></html>";
  3. Document doc = Jsoup.parse(html);
String html = "<html><head><title>First parse</title></head>"+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);

上面这个例子比较简单,直接就将html转换成Document实例了,之后再通过Elements,Element来进一步解析

再给大家贴一段我自己代码中使用的代码,仅供参考:

需要解析的html文件(只是body中的一段)

[html] view plain copy print?
  1. <div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;">
  2. <a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12997025-');        window.parent.checkinform('/china/ask?action=Chat-toSay-12997025-justRemove-1-',0,0);">
  3. <div style="float:left;">
  4. <img border="0" src="/image/1/39.png" width="30px" height="30px">
  5. </div>
  6. <div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;">
  7. <div style="float:left;">
  8. <font color="#800000" size="3px"> HaiFei-PC</font>
  9. </div>
  10. <div style="float:right;">
  11. 3天前
  12. </div>
  13. </div>
  14. <div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> 我爱你</div>
  15. </a>
  16. </div>
  17. <div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;">
  18. <a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12996969-');        window.parent.checkinform('/china/ask?action=Chat-toSay-12996969-justRemove-1-',0,0);">
  19. <div style="float:left;">
  20. <img border="0" src="/upload/userface/1/2/9/9/6/9/6/9/2.png" width="30px" height="30px">
  21. </div>
  22. <div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;">
  23. <div style="float:left;">
  24. <font color="#800000" size="3px"> ethen</font>
  25. </div>
  26. <div style="float:right;">
  27. 3天前
  28. </div>
  29. </div>
  30. <div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> [上传语音]</div>
  31. </a>
  32. </div>
  33. <div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;">
  34. <a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12996951-');        window.parent.checkinform('/china/ask?action=Chat-toSay-12996951-justRemove-1-',0,0);">
  35. <div style="float:left;">
  36. <img border="0" src="/image/2/7.png" width="30px" height="30px">
  37. </div>
  38. <div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;">
  39. <div style="float:left;">
  40. <font color="#800000" size="3px"> cooler</font>
  41. </div>
  42. <div style="float:right;">
  43. 4天前
  44. </div>
  45. </div>
  46. <div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> hello</div>
  47. </a>
  48. </div>
<div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;"><a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12997025-');        window.parent.checkinform('/china/ask?action=Chat-toSay-12997025-justRemove-1-',0,0);"><div style="float:left;"><img border="0" src="/image/1/39.png" width="30px" height="30px"></div><div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;"><div style="float:left;"><font color="#800000" size="3px"> HaiFei-PC</font></div><div style="float:right;">3天前</div></div><div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> 我爱你</div></a></div><div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;"><a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12996969-');       window.parent.checkinform('/china/ask?action=Chat-toSay-12996969-justRemove-1-',0,0);"><div style="float:left;"><img border="0" src="/upload/userface/1/2/9/9/6/9/6/9/2.png" width="30px" height="30px"></div><div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;"><div style="float:left;"><font color="#800000" size="3px"> ethen</font></div><div style="float:right;">3天前</div></div><div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> [上传语音]</div></a></div><div style="border-bottom:1px dashed #ececec;line-height:30px;width:250px;float:left;"><a href="javascript:void(0);" onclick="window.parent.RightShow('/china/ask?action=Chat-toSay-12996951-');     window.parent.checkinform('/china/ask?action=Chat-toSay-12996951-justRemove-1-',0,0);"><div style="float:left;"><img border="0" src="/image/2/7.png" width="30px" height="30px"></div><div style="height:15px;line-height:15px;display:block;text-overflow:clip;overflow:hidden;display:block;"><div style="float:left;"><font color="#800000" size="3px"> cooler</font></div><div style="float:right;">4天前</div></div><div style="height:15px;line-height:15px;width:200px;text-overflow:clip;overflow:hidden;display:block;"> hello</div></a></div>

MainActivity.java中的一个函数

[java] view plain copy print?
  1. private void parseHtmlUseJsoup(String html) {
  2. chatListModelList.clear();
  3. Document doc = Jsoup.parse(html);
  4. Element chatcontentElement = doc.getElementById("chatcontent");
  5. Elements chatElements = chatcontentElement.getElementsByTag("a");
  6. //String linkHref1 = chatList.attr("onclick");//window.parent.RightShow('/china/ask?action=Chat-toSay-12997025-');
  7. for (Element chatElement : chatElements) {
  8. ChatListModel chatListModel = new ChatListModel();
  9. // parse token id
  10. String tokenId = chatElements.attr("onclick");
  11. final String tokenIdPrefix = "/china/ask?action=Chat-toSay-";
  12. tokenId = tokenId.substring(tokenId.indexOf(tokenIdPrefix)+tokenIdPrefix.length(), tokenId.indexOf("-');"));
  13. chatListModel.setTokenId(tokenId);
  14. // parse image url
  15. Elements imgElements = chatElement.getElementsByTag("img");
  16. String imageUrl = imgElements.attr("src");
  17. imageUrl = NavigationUrl.getBaseUrl() + imageUrl;
  18. chatListModel.setImageUrl(imageUrl);
  19. // parse name time and content
  20. String chatElementText = chatElement.text(); // ethen 4天前 [上传语音]
  21. ///IWLog.d(TAG, "chatElementText=" + chatElementText);
  22. String[] str = chatElementText.split(" ");
  23. chatListModel.setFriendName(str[0]);
  24. chatListModel.setTime(str[1]);
  25. chatListModel.setContent(str[2]);
  26. chatListModelList.add(chatListModel);
  27. }
  28. //IWLog.d(TAG, "linkText=" + chatListModelList.toString());
  29. }
private void parseHtmlUseJsoup(String html) {chatListModelList.clear();Document doc = Jsoup.parse(html);Element chatcontentElement = doc.getElementById("chatcontent");Elements chatElements = chatcontentElement.getElementsByTag("a");//String linkHref1 = chatList.attr("onclick");//window.parent.RightShow('/china/ask?action=Chat-toSay-12997025-');for (Element chatElement : chatElements) {ChatListModel chatListModel = new ChatListModel();// parse token idString tokenId = chatElements.attr("onclick");final String tokenIdPrefix = "/china/ask?action=Chat-toSay-";tokenId = tokenId.substring(tokenId.indexOf(tokenIdPrefix)+tokenIdPrefix.length(), tokenId.indexOf("-');"));chatListModel.setTokenId(tokenId);// parse image urlElements imgElements = chatElement.getElementsByTag("img");String imageUrl = imgElements.attr("src");imageUrl = NavigationUrl.getBaseUrl() + imageUrl;chatListModel.setImageUrl(imageUrl);// parse name time and contentString chatElementText = chatElement.text(); // ethen 4天前 [上传语音]///IWLog.d(TAG, "chatElementText=" + chatElementText);String[] str = chatElementText.split(" ");chatListModel.setFriendName(str[0]);chatListModel.setTime(str[1]);chatListModel.setContent(str[2]);chatListModelList.add(chatListModel);}//IWLog.d(TAG, "linkText=" + chatListModelList.toString());}

以上代码仅用于参考,可能与您实际遇到的问题不一样。这里只是想表达Android可以利用jsoup来解析html

可以参考网址

http://jsoup.org/

http://www.open-open.com/jsoup/

转载于:https://www.cnblogs.com/vegetate/p/9997340.html

Android 利用jsoup解析 html相关推荐

  1. Android利用Jsoup解析html 开发网站客户端小记。

    这些天业余时间比较多,闲来无事,想起了以前看过开发任意网站客户端的一篇文章,就是利用jsoup解析网站网页,通过标签获取想要的内容.好了废话不多说,用到的工具为 jsoup-1.7.2.jar包,具体 ...

  2. Android开发探秘之三:利用jsoup解析HTML页面

    这节主要是讲解jsoup解析HTML页面.由于在android开发过程中,不可避免的涉及到web页面的抓取,解析,展示等等,所以,在这里我主要展示下利用jsoup jar包来抓取cnbeta.com网 ...

  3. Android利用Gson解析Json数据

    其实,要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个 ...

  4. Android 使用Jsoup解析网页批量获取图片

    Android 网络图片查看器HappyLook开发 一.前言 二.框架介绍 1.Jsoup简介 2.EventBus简介 3.RecyclerView及Glide 三.具体实现 1.需求确认 2.引 ...

  5. Android 使用Jsoup解析Html

    想要做一个看新闻的应用,类似Cnbeta客户端的东西.大致思路如下:根据链接获取新闻列表页的html代码,然后解析,找到所有的新闻标题和新闻链接用listView显示,当点击ListView的Item ...

  6. 利用Jsoup解析HTML

    http://passover.blog.51cto.com/2431658/484673 最近又要做html内容抽取,这次打算尝试一下除了用正则表达式以外的其他方式.自然第一个想到的就是HtmlPa ...

  7. 利用jsoup解析网站网页

    用到的工具为 jsoup-1.7.2.jar包,具体jsoup的相关文档,请去这边看http://jsoup.org/,这里有全部Api可以查询. 首先请求网页, Document doc = Jso ...

  8. Android利用jsoup爬虫爬网页数据(一)

    效果图太大了,我放到github上了,想看效果的点击以下链接: 效果图一 效果图二 首先这个jsoup只能对html爬取数据,js里面的东西爬不到,暂时先只爬html的数据,这里先说明一下,博主仅仅出 ...

  9. Android利用jsoup爬虫爬网页数据(二)

    效果图太大了,我放到github上了,想看效果的点击以下链接: 效果图一 效果图二 效果图和上文是一样的,上文只是说了一下简单的,这里的稍微麻烦一点,因为上文的基本就是个列表,而且数据结构比较简单,这 ...

最新文章

  1. ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl
  2. LVM逻辑卷管理测试——创建逻辑卷
  3. XXL-JOB v2.0.1 发布,分布式任务调度平台
  4. 现在的编译器还需要手动展开循环吗_一例 Go 编译器代码优化 bug 定位和修复解析...
  5. 1011:甲流疫情死亡率
  6. asp.net 生命周期中的时间流程
  7. c语言枚举法求满射函数,实变函数论讲义
  8. Java面试中经常被问到的问题有哪些?
  9. HashMap,LinkedHashMap和Hashtable类的深入剖析与理解
  10. js获取本机ip地址
  11. MATLAB----小波去噪
  12. pip的安装,卸载和换源
  13. 手机二维码应用潜力无限
  14. c语言qq自动回复,QQ自动回复
  15. [视频访谈翻译]David Kaneda访谈:移动HTML5设计和开发
  16. 一篇读懂jvm垃圾回收
  17. 如何清除/删除最近的文档历史记录?
  18. QQ飞车手游飞越狸想怎么获得
  19. 阿里实习生招聘笔试题目
  20. 伦敦备受青睐的标志性建筑——皇家阿尔伯特音乐厅

热门文章

  1. 《Forward团队-爬虫豆瓣top250项目-设计文档》
  2. linux基础-第十五单元 软件包的管理
  3. JavaScript对象的chapterI
  4. python for selenium 数据驱动测试
  5. 针对不同浏览器的css
  6. ls 命令还能这么玩?看一下这 20 个实用范例
  7. 【虾说区块链】入门区块链,先学分布式系统!一文说明分布式系统与区块链的关系...
  8. lucene4.7 高亮功能(八)
  9. 智慧城市建设面临“三座大山” 安全与服务需两手抓
  10. 【原创】packetbeat 之“request-response 错误关联”问题