zoukankan      html  css  js  c++  java
  • jsoup 提取 html 中的所有链接、图片和媒体

    原文:http://www.open-open.com/code/view/1420729333515

    package org.jsoup.examples;
    
    import org.jsoup.Jsoup;
    import org.jsoup.helper.Validate;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import java.io.IOException;
    
    /**
     * Example program to list links from a URL.
     */
    public class ListLinks {
        public static void main(String[] args) throws IOException {
            Validate.isTrue(args.length == 1, "usage: supply url to fetch");
            String url = args[0];
            print("Fetching %s...", url);
    
            Document doc = Jsoup.connect(url).get();
            Elements links = doc.select("a[href]");
            Elements media = doc.select("[src]");
            Elements imports = doc.select("link[href]");
    
            print("\nMedia: (%d)", media.size());
            for (Element src : media) {
                if (src.tagName().equals("img"))
                    print(" * %s: <%s> %sx%s (%s)",
                            src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
                            trim(src.attr("alt"), 20));
                else
                    print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
            }
    
            print("\nImports: (%d)", imports.size());
            for (Element link : imports) {
                print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
            }
    
            print("\nLinks: (%d)", links.size());
            for (Element link : links) {
                print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
            }
        }
    
        private static void print(String msg, Object... args) {
            System.out.println(String.format(msg, args));
        }
    
        private static String trim(String s, int width) {
            if (s.length() > width)
                return s.substring(0, width-1) + ".";
            else
                return s;
        }
    }
  • 相关阅读:
    记一次文件上传报错解决过程The temporary upload location is not vali
    Redis反序列化LocalDateTime时报错
    Redis连接池Lettuce Jedis 区别
    Java获取时间打印到控制台
    Java多线程之volatile关键字
    Java多线程之Future,有返回值的线程
    ccf集合竞价
    增广路径求解最大流
    2016年9月ccf
    hash表的建立和查找
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7048260.html
Copyright © 2011-2022 走看看