我正在尝试构建一个接收URL的方法,并读取网页上的所有文本以返回特定单词。到目前为止,我的测试方法看起来像这样:
public static String urlSuccessUnknown(String url) {
Document doc;
String res = null;
try {
doc = Jsoup.connect(url).get();
res = doc.body().text();
System.out.println(res);
if(res.indexOf("Dimmu Borgir") > 0)
return "METAL";
else
return "not metal :(";
}
catch (Exception e) { e.printStackTrace(); }
return "Unable to correctly parse";
}
但无论我测试什么,返回总是"不是金属:(",即使我传递的维基百科页面的URL应该返回" METAL"。我和#39; m测试网址:
https://www.bestbuy.com/
https://www.tamu.edu/
https://en.wikipedia.org/wiki/Dimmu_Borgir
最后一个链接肯定应该返回" METAL",但它没有。我错过了什么?
答案 0 :(得分:0)
来自Alex K。,
indexOf> = 0而不是> 0
另外,我测试了contains(),但它似乎不起作用。