Android 利用jsoup解析 html

时间:2022-11-01 08:16:37

原由:我在访问服务器时,回来的数据是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/ 大家可以学习一下。

简单附几句代码:

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中的一段)

<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中的一个函数

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 id
String 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 url
Elements imgElements = chatElement.getElementsByTag("img");
String imageUrl = imgElements.attr("src");
imageUrl = NavigationUrl.getBaseUrl() + imageUrl;
chatListModel.setImageUrl(imageUrl);

// parse name time and content
String 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/