zoukankan      html  css  js  c++  java
  • 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/
    大家可以学习一下。

    简单附几句代码:

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

    1. <div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> 我爱你</div>  
    15.     </a>  
    16.     </div>  
    17.     <div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> [上传语音]</div>  
    31.     </a>  
    32.     </div>  
    33.     <div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> hello</div>  
    47.     </a>  
    48.     </div>  
    <div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> 我爱你</div>
    	</a>
    	</div>
    	<div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> [上传语音]</div>
    	</a>
    	</div>
    	<div style="border-bottom:1px dashed #ececec;line-height:30px;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;200px;text-overflow:clip;overflow:hidden;display:block;"> hello</div>
    	</a>
    	</div>

    MainActivity.java中的一个函数

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

  • 相关阅读:
    烂泥:KVM使用NAT联网并为VM配置iptables端口转发
    烂泥:CentOS6.5挂载windows共享文件夹
    烂泥:KVM、kickstart与FTP集成
    js-浅显基础-正则表达式集
    小程序-轮播图案例
    小程序-TabBar点击切换
    js-禁止微信内置浏览器调整字体大小
    小程序-分享到群或好友
    小程序-提交信息(姓名,电话)
    js-在url后面添加时间戳清除浏览器打开页面的缓存
  • 原文地址:https://www.cnblogs.com/vegetate/p/9997340.html
Copyright © 2011-2022 走看看