问题描述
大家好,我如何才能统计html标签的总数而又不能一一统计?例如:
Hello guys how can I count the total number of html tags without counting them one by one? For example:
我不 想要这样的内容:
int count=0;
count=doc.getElementsByTag("img").size()+doc.getElementsByTag("div").size()+doc.getElementsByTag("a").size()+doc.getElementsByTag("p").size();
System.out.println("Scripts inside HTML " + count);
是否有一种方法可以在 Jsoup 中将所有这些标签计数到一个命令中?
Is there a way to count all these tags into one command in Jsoup?
推荐答案
有很多方法可以找到它.您可以使用document.select("*");
或org.jsoup.select.Collector
或document.getAllElements()
来获取所有元素.全部返回元素列表.该列表的大小给出了标签的数量.您也可以遍历元素并获取标签名称.或访问一个集合以查找不同的名称.下面的程序列出了所有这些.
There are many ways to find this. You can use document.select("*");
or org.jsoup.select.Collector
or document.getAllElements()
to get all the elements. All returns a list of elements. The size of that list gives the number of tags. Also you can iterate through the elements and get the tag name. Or atit to a set to find the distinct names. The below program list all these.
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Collector;
import org.jsoup.select.Evaluator;
public class CountTags {
public static void main(String[] args) {
String URL = "http://stackoverflow.com/";
try {
Document document = Jsoup.connect(URL).get();
List<String> tags = new ArrayList<String>();
System.out.println("Number of tags by getAllElements() method =" + document.getAllElements().size());
System.out.println("Number of tags by Collector.collect() method =" + Collector.collect(new Evaluator.AllElements(), document).size());
System.out.println("Number of tags by select(\"*\") method =" + document.select("*").size());
for(Element e : document.getAllElements()){
tags.add(e.tagName().toLowerCase());
}
System.out.println("The tags = " + tags);
System.out.println("Distinct tags = " + new HashSet<String>(tags));
} catch (IOException e) {
e.printStackTrace();
}
}
}
这篇关于如何使用Jsoup计算HTML标签总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!