关于html标签中 的过滤问题
自从工作以来经常被过滤 所困扰
我肯定不需要整个html的文字内容,所以我不需要整个区过滤
我只需要解析出有效数据就可以了
比如:
<p class="islove">
<a href="javascript:void(0);" style="width:60px" class="btn_orange like_click" id="l_297795"><em class="ico20"> </em>喜欢</a>
<span><b class="ico12"> </b>34</span>
</p>
这个标签,我只想取到34这个值
步骤如下:
假设html=网页采集结果
Document doc=Jsoup.parse(html);
String str=doc.getElementsByClass("islove").get(0).getElementsByTag("span").text();
str=str.replace(" ","");
System.out.println(str);
得到的结果是" 34";
如果加上
str=str.trim();
结果依然是" 34";
这样一来,在进行Integer.parseInt(str)的时候肯定就报错了;为什么呢?
仔细分析会发现:
String str=doc.getElementsByClass("islove").get(0).getElementsByTag("span").text();
这句代码得到的str你打印出来是什么
" 34"
这个时候你再去str=str.replace(" ","")是不起任何作用 的;
正确的做法是:
Document doc=Jsoup.parse(html);
String str=doc.getElementsByClass("islove").get(0).html().replace(" ","");
doc=Jsoup.parse(str);
str=doc.getElementsByTag("span").text();
System.out.println(str);
这样打印的结果就是我们想要的"34";
记下来,不要忘记了
自从工作以来经常被过滤 所困扰
我肯定不需要整个html的文字内容,所以我不需要整个区过滤
我只需要解析出有效数据就可以了
比如:
<p class="islove">
<a href="javascript:void(0);" style="width:60px" class="btn_orange like_click" id="l_297795"><em class="ico20"> </em>喜欢</a>
<span><b class="ico12"> </b>34</span>
</p>
这个标签,我只想取到34这个值
步骤如下:
假设html=网页采集结果
Document doc=Jsoup.parse(html);
String str=doc.getElementsByClass("islove").get(0).getElementsByTag("span").text();
str=str.replace(" ","");
System.out.println(str);
得到的结果是" 34";
如果加上
str=str.trim();
结果依然是" 34";
这样一来,在进行Integer.parseInt(str)的时候肯定就报错了;为什么呢?
仔细分析会发现:
String str=doc.getElementsByClass("islove").get(0).getElementsByTag("span").text();
这句代码得到的str你打印出来是什么
" 34"
这个时候你再去str=str.replace(" ","")是不起任何作用 的;
正确的做法是:
Document doc=Jsoup.parse(html);
String str=doc.getElementsByClass("islove").get(0).html().replace(" ","");
doc=Jsoup.parse(str);
str=doc.getElementsByTag("span").text();
System.out.println(str);
这样打印的结果就是我们想要的"34";
记下来,不要忘记了