2014-02-26 26 views
0

我想讀一個網頁有多個頁面,例如:頁面= 1至100錯誤java.io.FileNotFoundException,則在瀏覽網頁

import org.htmlcleaner.*; 
... 
url = http://www.webpage.com/search?id=10&page=1 

for (int j = 1; j <= 100; j++) { 
    WebParse thp = new WebParse(new URL(url+j)); 

有時候,我得到以下錯誤:

java.io.FileNotFoundException: http://www.webpage.com/search?id=10&page=18 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at java.net.URL.openStream(Unknown Source) 
    at org.htmlcleaner.Utils.readUrl(Utils.java:63) 
    at org.htmlcleaner.HtmlCleaner.clean(HtmlCleaner.java:373) 
    at org.htmlcleaner.HtmlCleaner.clean(HtmlCleaner.java:387) 
    at <mypackage>.WebParse.<init>(WebParse.java:21) 
    at <mypackage>.WebParse.runThis(WebParse.java:54) 
    at <mypackage>.WebParse.main(WebParse.java:43) 

我認爲這個問題是由我的網絡連接造成的,因爲當我嘗試刷新(重新運行)時,它的工作很好。

如何在發生此錯誤時自動嘗試重新運行。

+0

是的,也許...... – Antoniossss

+0

順便說一句,如果你的url變量是你從頁面會11〜199尾頁= 1,跳過1-10 –

+0

什麼是WebParse頁? –

回答

1

爲什麼不添加一些嘗試和他們之間的一點點延遲?

for (int j = 1; j <= 100; j++) { 
     int maxretries = 3; 
     int attempts = 0; 
     boolean success = false; 
     while (attempts < maxretries && !success) { 
      attempts++; 
      try { 
       WebParse thp = new WebParse(new URL(url + j)); 
       success = true; 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
       try { 
        Thread.sleep(1000); // play nice 
       } catch (InterruptedException e1) { 
        e1.printStackTrace(); 
       } 
      } 
     } 

    } 
+0

解決Thread.sleep問題不是很好的方法。最好檢查一下WebParse的工作原理以及爲什麼它有如此簡單的任務出現問題。 –

+0

@JakubHr同意。我只是從堆棧跟蹤中假設他的WebParse類是什麼(因爲我找不到任何有關web上的包「gadgetshop」的參考)只是清除了來自常規URLConnection的html。但是如果他能向我們提供更多關於它是如何實施的細節,我們可以避免潛在的線程問題 – Leo

+0

我只是改變「gadgetshop」這是我的包名稱。 – enhaka