2017-04-22 60 views
0

:我没有那么深入Java。jsoup URL首先通过变量

我试图做到这一点我创建了一个字符串变量用扫描仪(System.in),并使用此为Document doc = Jsoup.connect(variable instead of "www.url.com").get();

这是我到目前为止的代码,希望我不要忘记什么都不重要。

首先Java的类在那里我得到的扫描仪和可变

public static String inputURL() { 
     System.out.print("Please insert a valid URL to crawl: "); 
     String input = scanner.nextLine(); 
     System.out.println("Output: " + input); 

     return input; 
    } 

在此之后我运行static String input = Crawler.inputURL();

在其它与Java类中的方法,然后想用Document doc = Jsoup.connect(input).get();

连接

我收到以下错误:

Exception in thread "main" java.lang.IllegalArgumentException:    Malformed URL: www.google.de 
    at org.jsoup.helper.HttpConnection.url(HttpConnection.java:101) 
    at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:43) 
    at org.jsoup.Jsoup.connect(Jsoup.java:73) 

希望有人能帮助我。

整个事情工作,如果我不使用变量,但写入的URL。

回答

0

Jsoup.coonect方法期望URL以http://https://开头。将此前缀添加到您的网址,即可完成。

+0

非常感谢。有用! – ShanghaiTang