生活随笔
收集整理的這篇文章主要介紹了
Android 利用jsoup解析 html
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原由:我在訪問服務(wù)器時(shí),回來的數(shù)據(jù)是html源文件,我需要從html源文件中獲取對自己有用的信息。
一般情況下jsoup多用于JAVA方面的開發(fā),可以利用jQuery,DOM一同使用。可以達(dá)到抓取其它網(wǎng)站的信息。
好了開始介紹如何使用jsoup在android中解析html的使用
1. 下載jsoup包?http://jsoup.org/download
jsoup官網(wǎng):http://jsoup.org/
我這里下載的是jsoup-1.7.2.jar ?core library
2. 將下載下來的jar包放到放到Android工程中 libs目錄下
在ADT稍微新一點(diǎn)的版本,放到libs下刷新一下,就可以用了
如果ADT比較老可能需要自己手動引入一下jar包(這個(gè)自己網(wǎng)上查一下)
3.在代碼中使用
這個(gè)是一個(gè)中文開發(fā)手冊 http://www.open-open.com/jsoup/ 大家可以學(xué)習(xí)一下。
簡單附幾句代碼:
[java] view plain
copy print ?
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);上面這個(gè)例子比較簡單,直接就將html轉(zhuǎn)換成Document實(shí)例了,之后再通過Elements,Element來進(jìn)一步解析
再給大家貼一段我自己代碼中使用的代碼,僅供參考:
需要解析的html文件(只是body中的一段)
[html] view plain
copy print ?
< 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中的一個(gè)函數(shù)
[java] view plain
copy print ?
private ?void ?parseHtmlUseJsoup(String?html)?{??????????????chatListModelList.clear();?? ?????????????? ????????????Document?doc?=?Jsoup.parse(html);?? ????????????Element?chatcontentElement?=?doc.getElementById("chatcontent" );?? ????????????Elements?chatElements?=?chatcontentElement.getElementsByTag("a" );?? ?????????????? ?????????????? ????????????for ?(Element?chatElement?:?chatElements)?{?? ????????????????ChatListModel?chatListModel?=?new ?ChatListModel();?? ?????????????????? ????????????????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);?? ?????????????????? ?????????????????? ????????????????Elements?imgElements?=?chatElement.getElementsByTag("img" );?? ????????????????String?imageUrl?=?imgElements.attr("src" );?? ????????????????imageUrl?=?NavigationUrl.getBaseUrl()?+?imageUrl;?? ????????????????chatListModel.setImageUrl(imageUrl);?? ?????????????????? ?????????????????? ????????????????String?chatElementText?=?chatElement.text();??? ?????????????????? ????????????????String[]?str?=?chatElementText.split("?" );?? ????????????????chatListModel.setFriendName(str[0 ]);?? ????????????????chatListModel.setTime(str[1 ]);?? ????????????????chatListModel.setContent(str[2 ]);?? ????????????????chatListModelList.add(chatListModel);?? ????????????}?? ?????????????? ?? ????????}?? 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());}
以上代碼僅用于參考,可能與您實(shí)際遇到的問題不一樣。這里只是想表達(dá)Android可以利用jsoup來解析html
可以參考網(wǎng)址
http://jsoup.org/
http://www.open-open.com/jsoup/
轉(zhuǎn)載于:https://www.cnblogs.com/vegetate/p/9997340.html
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生
總結(jié)
以上是生活随笔 為你收集整理的Android 利用jsoup解析 html 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。