我遇到了Jsoup和悬挂的问题。我正在测试以前工作的Jsoup代码,当它无处不在时它停止工作。我大约一周前没有更改任何代码,直到现在,它一直在工作。
我一直试图点击维基百科的主页,以便从家庭作业中删除它。
它挂起而不会丢失任何错误,程序也不会超过URL连接.get()
方法。我等了大约10分钟,但仍然没有发生任何事情。
以下是我的代码:
private WikiPage pullData(String url, WikiPage parent) {
WikiPage wp;
try {
String decodedURL = URLDecoder.decode(url, "UTF-8");
Document doc = Jsoup.connect(decodedURL).get();
Elements links = doc.select("a");
Elements paragraphs = doc.select("p");
Element t = doc.select("title").first();
StringBuilder words = new StringBuilder();
String title = t.text().replace(" - Wikipedia", "");
paragraphs.forEach(e -> {
words.append(e.text().toLowerCase());
});
wp = new WikiPage(url, title, parent);
for (int i = 0; i < AMOUNT_LINKS; i++) {
boolean properLink = false;
while (!properLink) {
//int rnd = R_G.nextInt(links.size());
String a = links.get(i).attr("href");
if (a.length() >= 5 && a.substring(0, 5).equals("/wiki") && containsChecker(a)) {
String BASE_URL = "https://en.wikipedia.org";
String decode = URLDecoder.decode(BASE_URL + a, "UTF-8");
wp.addChildren(decode);
properLink = true;
}
}
}
String[] splitWords = words.toString().replaceAll("[_$&+,:;=?@#|'<>.^*()%!\\[\\]\\-\"/{}]", " ").split(" ");
for (String s : splitWords) {
if (s.length() >= 1) {
wp.addToWords(new WordCount(s, 1, 0));
}
}
System.out.printf("%1$-10s %2$-45s\n", counter, title);
counter++;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return wp;
}
以下是运行程序10分钟后的截图,其中包含Elements links = doc.select("a");
处的断点:
Before hitting the .get()
method
我似乎无法看到问题出在哪里,我甚至尝试过不同的网站,但它根本不起作用。
感谢您的帮助!