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
我不知道为什么会发生这种情况?有人能让我知道可能出现的问题吗?
答案 0 :(得分:1)
您从所有div class="chart-row__image
元素中提取样式属性字符串,但要了解该组中的许多元素都没有样式属性。在这种情况下,JSoup返回一个空字符串,这会搞乱你的程序。解决方案不是这样做,而是让jsoup只选择那些具有style属性的元素。
例如,不是:
Elements urls = doc.getElementsByClass("chart-row__image");
而是:
Elements urls = doc.select(".chart-row__image[style]");
是的,不要忽视例外。
所以
String url = "http://www.billboard.com/charts/artist-100";
String urlFound;
String closing = ")";
String start = "h";
Document doc;
try {
doc = Jsoup.connect(url).get();
// Elements urls = doc.getElementsByClass("chart-row__image");
Elements urls = doc.select(".chart-row__image[style]");
for (Element u : urls) {
urlFound = u.attr("style");
int startingIndex = urlFound.indexOf(start);
int endingIndex = urlFound.indexOf(closing);
if (startingIndex > 0 && endingIndex > 0) {
String sub = urlFound.substring(startingIndex, endingIndex);
System.out.println(sub);
}
}
} catch (IOException e) {
e.printStackTrace();
}