主:
public static void main(String args[]){
PicParcer c = new PicParcer();
strHtml = c.getHTML(strMainLink);
links = c.poimeyArray(strHtml);
space = c.spaceDel(links);
/*
for (String strHtml1 : strHtml) {
System.out.println(strHtml);
}*/
for (String strHtml1 : links) {
System.out.println(strHtml1);
}
for (String link : space) {
System.out.println(link);
}
System.out.println(strHtml.size());
System.out.println(links.size());
}
getHTML方法;
public ArrayList<String> getHTML(String urlToRead) { //получаем сайт
ArrayList<String> html = new ArrayList<String>();
URL url;
HttpsURLConnection conn;
BufferedReader reader;
String line; /// очень нужная фигня!!!!!
try {
url = new URL(urlToRead);
conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((line=reader.readLine()) != null) {
html.add(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return html;
}
public static final String strJPG =“.jpg”;
public ArrayList<String> spaceDel(ArrayList<String> htmlArray){
ArrayList<String> spaceDel = new ArrayList<String>();
for(int i=0; i< htmlArray.size();i++){
spaceDel.add(htmlArray.get(i).substring(htmlArray.get(i).indexOf("/"), htmlArray.get(i).indexOf(strJPG)));
}
return spaceDel;
}
在此示例中,我有StringIndexOutOfBoundsException异常,如果我将strJPG(字符串)更改为“。”或者使用try-catch程序是有效的。我做错了什么?
字符串示例:
<img src="/b/thumb/88785978/14267775060770s.jpg" width="220" height="123" alt="87" class="img preview ">
<img src="/b/thumb/88785978/14267776383080s.jpg" width="220" height="123" alt="94" class="img preview ">
<img src="/b/thumb/88785978/14267776539240s.jpg" width="208" height="220" alt="32" class="img preview ">
<img src="/b/thumb/88785978/14267788646090s.jpg" width="204" height="220" alt="130" class="img preview ">
<img src="/b/thumb/88785978/14267792210110s.jpg" width="220" height="146" alt="44" class="img preview ">
<img src="/b/thumb/88785978/14267793068690s.jpg" width="220" height="123" alt="711" class="img preview ">
<img src="/b/thumb/88785978/14267794678710s.jpg" width="220" height="220" alt="54" class="img preview ">
<img src="/b/thumb/88785978/14267795607190s.jpg" width="220" height="214" alt="95" class="img preview ">
<img src="/b/thumb/88785978/14267800381730s.jpg" width="220" height="192" alt="126" class="img preview ">
<img src="/b/thumb/88785978/14267806796140s.jpg" width="220" height="124" alt="4612" class="img preview ">
<img src="/b/thumb/88785978/14267832300390s.jpg" width="150" height="109" alt="2" class="img preview ">
答案 0 :(得分:0)
我在一个简短的例子中试过你的代码
package de.pageto.mavenproject1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringSplitter {
public static void main(String[] args) {
String[] data = {"<img src=\"/b/thumb/88785978/14267775060770s.jpg\" width=\"220\" height=\"123\" alt=\"87\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267776383080s.jpg\" width=\"220\" height=\"123\" alt=\"94\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267776539240s.jpg\" width=\"208\" height=\"220\" alt=\"32\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267788646090s.jpg\" width=\"204\" height=\"220\" alt=\"130\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267792210110s.jpg\" width=\"220\" height=\"146\" alt=\"44\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267793068690s.jpg\" width=\"220\" height=\"123\" alt=\"711\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267794678710s.jpg\" width=\"220\" height=\"220\" alt=\"54\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267795607190s.jpg\" width=\"220\" height=\"214\" alt=\"95\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267800381730s.jpg\" width=\"220\" height=\"192\" alt=\"126\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267806796140s.jpg\" width=\"220\" height=\"124\" alt=\"4612\" class=\"img preview \">",
"<img src=\"/b/thumb/88785978/14267832300390s.jpg\" width=\"150\" height=\"109\" alt=\"2\" class=\"img preview \">"
};
System.out.println("Result: " + spaceDel(Arrays.asList(data)));
}
public static ArrayList<String> spaceDel(List<String> htmlArray) {
final String strJPG = ".jpg";
ArrayList<String> spaceDel = new ArrayList<>();
for (String str : htmlArray) {
spaceDel.add(str.substring(str.indexOf("/"), str.indexOf(strJPG)));
}
return spaceDel;
}
}
一切正常。在我看来,您尝试将<img>
标记与HTML页面分开。这些标签可以具有其他图像格式.jpg
(即png,gif或jpeg)。
您必须先检查数据。