gpt4 book ai didi

java - 错误java.io.FileNotFoundException,读取网页

转载 作者:行者123 更新时间:2023-12-01 13:20:40 25 4
gpt4 key购买 nike

我想读取一个多页的网页,例如:page=1到100

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)

我认为这个问题是由我的网络连接引起的,因为当我尝试刷新(重新运行)时,有时效果很好。

如何让它在发生此错误时自动尝试重新运行。

最佳答案

为什么不添加一些尝试并在它们之间添加一点延迟?

    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();
}
}
}

}

关于java - 错误java.io.FileNotFoundException,读取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com