java - 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));
不明白为什么
解决方案
您试图ports
从表格的第二列和ips
第一列中提取 ,但最终得到的结果ips
超过ports
.
我的猜测是 HTML 中的某个地方有另一个表,它只有一列(其中有一行)。
其他可能性包括该表在其中一列中缺少 TD。
推荐阅读
- ios - 如果应用程序在后台停留超过 30 秒而不是在进入后台时显示屏幕,则应用程序从主屏幕启动
- rust - 基本 Rust 代码编译并运行,但 CLion 给出移动错误
- sql - SQL 服务器上的正则表达式替换或 LISTAGG
- r - evaluate the aggregate of percentage in variable
- python - CNN结构扩展错误(Error:Negative dimension size 由于'{{node max_pooling2d_2)从1中减去2引起的
- spring-boot - 在 spring webflux 应用程序中更改相同站点会话 cookie 属性
- python - 如何在python的for循环中停止重复?
- webpack - 将 webpack 从 4 升级到 5 后调试失败
- java - ActionListener 不能在其中包含循环
- php - Laravel 8 数组到字符串的转换