JSOUP上的java获取索引不工作
try {
String url = "http://www.billboard.com/charts/artist-100";
String urlFound;
String closing = ")";
String start = "h";
Document doc = Jsoup.connect(url).get();
Elements urls = doc.getElementsByClass("chart-row__image");
for (Element u : urls) {
urlFound = u.attr("style");
String sub = urlFound.substring(urlFound.indexOf(start), urlFound.indexOf(closing));
System.out.println(sub);
//Log.d("URLS,", attr.substring(attr.indexOf("http://"), attr.indexOf(")")));
}
}
catch(IOException ex){
}
我试着调试了好几次,但是我一直得到错误,Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
我也不知道为什么会发生这种情况?有人能告诉我什么地方出了问题吗
# 1 楼答案
您正在从所有的
div class="chart-row__image
元素中提取样式属性字符串,但是要知道,这个组中的许多元素都没有样式属性。在这种情况下,JSoup将返回一个空字符串,这将打乱您的程序。解决方案不是这样做,而是让jsoup只选择那些具有style属性的元素例如,不是:
而是:
是的,不要忽略例外情况
所以