2016-10-01 54 views
1
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我不知道爲什麼會發生這種情況嗎?有人能告訴我什麼可能是錯的嗎?獲取索引JSOUP不工作

回答

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