简体   繁体   中英

I have an exception in thread “main” java.lang.IllegalArgumentException with jsoup

package asdf;
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;
public class asdasd {
    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;
        }




    }

This is my code. I am using jsoup and it's give me an error like this:

Exception in thread "main" java.lang.IllegalArgumentException: usage: supply url to fetch
    at org.jsoup.helper.Validate.isTrue(Validate.java:45)
    at asdf.asdasd.main(asdasd.java:11)

Can anyone help?

Looks like the Validate.isTrue method is checking if the args.length == 1 , seeing that it's not, and throwing IllegalArgumentException as a result.

Therefore, if you pass in one argument, your argument array will look like this:

arg[0] == "passed_argument"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM