2014-02-26 27 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