2013-02-20 135 views
1

我試圖用JSoup連接到一個URL。以.jhtml結尾的JSoup獲取URL

當我使用下面的代碼

Jsoup.connect("http://www.example.com/").get(); 
Jsoup.connect("http://www.example.com/example.html").get(); 

第一次調用成功,第二個會引發異常:

Exception in thread "main" java.lang.IllegalArgumentException: Malformed URL: http://www.example.com/example.html 
    at org.jsoup.helper.HttpConnection.url(HttpConnection.java:55) 
    at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:27) 
    at org.jsoup.Jsoup.connect(Jsoup.java:73) 
    at webscraper.JsoupTest.main(JsoupTest.java:259) 
Caused by: java.net.MalformedURLException: no protocol: http://www.example.com/example.html 
    at java.net.URL.<init>(URL.java:567) 
    at java.net.URL.<init>(URL.java:464) 
    at java.net.URL.<init>(URL.java:413) 
    at org.jsoup.helper.HttpConnection.url(HttpConnection.java:53) 
    ... 3 more 

我相信它有什麼東西在URL做名「.html」 。

如何使用JSoup連接到URL?

回答

2

它無關。html的,你有你的第二個URL一個空格前綴。 刪除空白處,它會在我身邊正常工作。

您可以使用此tool的ASII轉換爲十六進制的代碼行seperately,你會發現第二個URL對的16

+0

你是對的。謝謝!你知道我可以如何移除它嗎? 'url.replaceAll(「\\ s」,「」)'在這裏沒有幫助。 – 2013-02-20 16:15:06

+0

很奇怪,它不是一個普通的空白'20',它是'1f'。也許你可以用正確的'http://'替換'http://'(用奇怪的代碼)。 – Joe 2013-02-20 16:18:50

+1

好的提示。我這樣做:'url =「http」+ url.substring(url.indexOf(「:」));' – 2013-02-20 17:57:42