爬虫抽取网页中同层级多标签元素

public static String getDesc(Document doc) {
        String desc = "";
        Elements element_contents_all = doc.select("h1,h2,blockquote,p");
        for (Element element : element_contents_all) {
            desc += element.text() + " ";
            if (desc.length() >= 400) {
                logger.info("\t内容超过400字,停止抽取。");
                logger.info(desc);
                break;
            }
        }
        return desc;
    }

相关推荐