我试图从URL中提取标题和元标记的描述内容,这就是我所拥有的:
fin[] //urls in a string array
for (int f = 0; f < fin.length; f++)
{
Document finaldoc = Jsoup.connect(fin[f]).get(); //fin[f] contains url at each instance
Elements finallink1 = finaldoc.select("title");
out.println(finallink1);
Elements finallink2 = finaldoc.select("meta");
out.println(finallink2.attr("name"));
out.println(fin[f]); //printing url at last
}
Run Code Online (Sandbox Code Playgroud)
但它不打印标题,只是将描述打印为"描述"并打印网址.
结果:
description
plus.google.com
generator
en.wikipedia.org/wiki/google
description
earth.google.com
你好我们试图通过for循环打印字符串数组中的一组url中的标题,而打印时,在某些情况下无法建立连接并引发异常,这会阻止所有剩余的元素....
我正在使用此代码进行打印,有人可以帮助我忽略异常并继续使用字符串数组中的另一个元素循环...
for (int f = 0; f < fin.length; f++) //fin be string array of urls
{
Document finaldoc = Jsoup.connect(fin[f]).get();
out.println(finaldoc.title());
}
Run Code Online (Sandbox Code Playgroud)
其中一个例外是
java.net.SocketTimeoutException: Read timed out
Run Code Online (Sandbox Code Playgroud) 我试图从字符串中删除最后一个字符,如果它是/.我正在使用字符串数组temp[]来存储字符串.
这是我的代码:
char ch = ' ';
for (int st = 0; st < temp.length; st++)
{
ch = temp[st].charAt(temp[st].length()-1);
if (ch == '/')
temp[st] = temp[st].substring(0, temp[st].length()-1);
result2.append(temp[st]);
}
Run Code Online (Sandbox Code Playgroud)
但我得到了
StringIndexOutOfBoundsException -1
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?