纯手打,转载务请附上本文网址!!!
什么是Jsoup,Java代码解析html页面的工具;先把html页面解析为字符串,再用Jsoup解析即可
用这个小东西可以实现Java爬虫的哦,有意思吧~
附上github地址:https://github.com/xianzhixianzhixian/RedisDemo.git
首先我要分析的是这样一个页面:
获得红框中的几个数字,而红框那一行对应的html代码
<tfoot>
<tr>
<td>Total</td>
<td class="bar">31,606 of 40,042</td>
<td class="ctr2">21%</td>
<td class="bar">1,463 of 1,695</td>
<td class="ctr2">14%</td>
<td class="ctr1">2,930</td>
<td class="ctr2">4,026</td>
<td class="ctr1">6,451</td>
<td class="ctr2">8,746</td>
<td class="ctr1">2,119</td>
<td class="ctr2">3,173</td>
<td class="ctr1">83</td>
<td class="ctr2">199</td>
</tr>
</tfoot>
我对应的Java代码如下(已经运行测试通过的代码哦)
File file=new File("/Users/fanxianzhifanxianzhi/Desktop/20180102163706/index.html");
Document document= Jsoup.parse(file,"UTF-8");
Element element=document.getElementsByTag("tfoot").get(0);
List<Element> elements=element.getElementsByTag("td");
double up=0;
double down=0;
Map<String,String> avgCoverageMap=new HashMap<>();
String branch=elements.get(3).text();
String[] branchArray=branch.split(" ");
NumberFormat numberFormat=NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
up=Double.parseDouble(branchArray[0].replaceAll(",",""));
down=Double.parseDouble(branchArray[2].replaceAll(",",""));
avgCoverageMap.put("branch",numberFormat.format((down-up)/down*100));
up=Double.parseDouble(elements.get(7).text().replaceAll(",",""));
down=Double.parseDouble(elements.get(8).text().replaceAll(",",""));
avgCoverageMap.put("line",numberFormat.format((down-up)/down*100));
up=Double.parseDouble(elements.get(9).text().replaceAll(",",""));
down=Double.parseDouble(elements.get(10).text().replaceAll(",",""));
avgCoverageMap.put("method",numberFormat.format((down-up)/down*100));