且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Jsoup计算HTML标签总数

更新时间:2023-12-03 20:36:04

有很多方法可以找到它.您可以使用document.select("*");org.jsoup.select.Collectordocument.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://***.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();
        }
    }

}