首页 > 解决方案 > java.lang.IndexOutOfBoundsException:索引:308,大小:308

问题描述

    HtmlPage page = null;
    List <HtmlElement> ips = null ;
    List <HtmlElement> ports = null ; 

    ArrayList <String> proxies = new ArrayList();
    String temp;
    String baseUrl = "https://free-proxy-list.net/" ;
    WebClient client = new WebClient();
    client.getOptions().setCssEnabled(false);
    client.getOptions().setJavaScriptEnabled(false);

    try{
        page = client.getPage(baseUrl);

        ips = page.getByXPath("//table/tbody/tr/td[position()=1]/text()");
        ports = page.getByXPath("//table/tbody/tr/td[position()=2]/text()");

        for(int i=0;i<ips.size();i++){
            System.out.println("IP: "+ips.get(i));

            temp=(ips.get(i)+":"+ports.get(i));
            System.out.println("Temp: "+temp);
            if(!(temp.matches(".*[a-z].*"))){
                proxies.add(temp);
            }
       }
        for (int i=0;i<ips.size();i++){
            System.out.println(proxies.get(i));
         }

   }

    catch(Exception e){
     e.printStackTrace();
   }

你好,我java.lang.IndexOutOfBoundsException: Index: 308, Size: 308在这段话中不断收到错误:temp=(ips.get(i)+":"+ports.get(i)); 不明白为什么

标签: java

解决方案


您试图ports从表格的第二列和ips第一列中提取 ,但最终得到的结果ips超过ports.

我的猜测是 HTML 中的某个地方有另一个表,它只有一列(其中有一行)。

其他可能性包括该表在其中一列中缺少 TD。


推荐阅读