解析 URL 以获取 hyber link 并将 hyber link 存储在单独的变量中
Parsing URL to get the hyber link and store the hyber link in separate variable
您好,我正在 URL 解析 jsoup。当我解析 URL 时,我想获取 table 中的所有 href
并将每个 href
存储在单独的变量中。我的 Java 代码在这里
String url=null;
try
{
doc=Jsoup.connect("http://livechennai.com/powershutdown_news_chennai.asp").get();
Elements table=doc.select("#table13>tbody>tr>td>a");
for(Element link:table){
url=link.attr("abs:href");
System.out.println(url);
}
}
catch(IOException e){
e.printStackTrace();
}
我的输出如下所示
http://livechennai.com/detailnews.asp?newsid=18318
http://livechennai.com/detailnews.asp?newsid=18318
http://livechennai.com/detailnews.asp?newsid=18112
http://livechennai.com/detailnews.asp?newsid=18112
http://livechennai.com/detailnews.asp?newsid=18006
http://livechennai.com/detailnews.asp?newsid=18006
http://livechennai.com/detailnews.asp?newsid=17556
http://livechennai.com/detailnews.asp?newsid=17556
http://livechennai.com/detailnews.asp?newsid=17454
http://livechennai.com/detailnews.asp?newsid=17454
如何将每个 href link 存储在单独的变量中或任何其他方式都是可能的。帮我得到准确的答案。
这里是一个小片段,说明如何将 href
元素的 URL 添加到 List
List<String> hrefs = new ArrayList<>();
try {
Document doc = Jsoup.connect("http://livechennai.com/powershutdown_news_chennai.asp").get();
// more specific element specification, as in the question
// Elements table = doc.select("#table13>tbody>tr>td>a");
// less specific as mentioned by Jonathan
Elements table = doc.select("#table13 a");
for (Element link : table) {
hrefs.add(link.attr("abs:href"));
}
} catch (IOException e) {
e.printStackTrace();
}
// here you can process all hrefs
for (String href : hrefs) {
System.out.println(href);
}
您好,我正在 URL 解析 jsoup。当我解析 URL 时,我想获取 table 中的所有 href
并将每个 href
存储在单独的变量中。我的 Java 代码在这里
String url=null;
try
{
doc=Jsoup.connect("http://livechennai.com/powershutdown_news_chennai.asp").get();
Elements table=doc.select("#table13>tbody>tr>td>a");
for(Element link:table){
url=link.attr("abs:href");
System.out.println(url);
}
}
catch(IOException e){
e.printStackTrace();
}
我的输出如下所示
http://livechennai.com/detailnews.asp?newsid=18318
http://livechennai.com/detailnews.asp?newsid=18318
http://livechennai.com/detailnews.asp?newsid=18112
http://livechennai.com/detailnews.asp?newsid=18112
http://livechennai.com/detailnews.asp?newsid=18006
http://livechennai.com/detailnews.asp?newsid=18006
http://livechennai.com/detailnews.asp?newsid=17556
http://livechennai.com/detailnews.asp?newsid=17556
http://livechennai.com/detailnews.asp?newsid=17454
http://livechennai.com/detailnews.asp?newsid=17454
如何将每个 href link 存储在单独的变量中或任何其他方式都是可能的。帮我得到准确的答案。
这里是一个小片段,说明如何将 href
元素的 URL 添加到 List
List<String> hrefs = new ArrayList<>();
try {
Document doc = Jsoup.connect("http://livechennai.com/powershutdown_news_chennai.asp").get();
// more specific element specification, as in the question
// Elements table = doc.select("#table13>tbody>tr>td>a");
// less specific as mentioned by Jonathan
Elements table = doc.select("#table13 a");
for (Element link : table) {
hrefs.add(link.attr("abs:href"));
}
} catch (IOException e) {
e.printStackTrace();
}
// here you can process all hrefs
for (String href : hrefs) {
System.out.println(href);
}