Android 利用jsoup解析 html
生活随笔
收集整理的這篇文章主要介紹了
Android 利用jsoup解析 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);?? 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來進一步解析 <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>?? <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());?? ?? ????????}?? 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
原由:我在訪問服務器時,回來的數據是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?
再給大家貼一段我自己代碼中使用的代碼,僅供參考:
需要解析的html文件(只是body中的一段)
[html] view plain copy print?
MainActivity.java中的一個函數
[java] view plain copy print?
以上代碼僅用于參考,可能與您實際遇到的問題不一樣。這里只是想表達Android可以利用jsoup來解析html
可以參考網址
http://jsoup.org/
http://www.open-open.com/jsoup/
轉載于:https://www.cnblogs.com/vegetate/p/9997340.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Android 利用jsoup解析 html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode题解(22)
- 下一篇: Android Studio 中快速提取