简体   繁体   English

Jsoup-如何在一个数组中获取所有链接和标题?

[英]Jsoup - How can i get all links and titles in one array?

this is my first question so far. 到目前为止,这是我的第一个问题。 I need to get links and titles from certain html page in one 2D Array. 我需要在一个2D数组中从某些HTML页面获取链接和标题。 Here is my code: 这是我的代码:

public String[][] data;
descs = doc.select("a"); 
data= new String [spaceCount][2];
int count=0;
            for (Element e : descs ) {
                data[count][0]=descs.attr("href");
                data[count][1]=descs.attr("title");
                count++;
            } 
String svalues = data[0][0]+"\n"+data[0][1]+data[1][0]+"\n"+data[1][1];
output.setText(svalues);

But my problem is that it keeps getting the same data in every place. 但是我的问题是,它总是在每个地方获取相同的数据。 I mean that in every cell here is only one, same link and one, same title. 我的意思是,每个单元格中只有一个相同的链接和一个相同的标题。 I am newbie in java, but I think things in loop are not moving (and they should). 我是Java的新手,但我认为循环中的事物没有移动(它们应该)。 Can anyone explain how to make it work? 谁能解释一下如何使其工作?

You are not using Element e . 您没有使用Element e Change 更改

           data[count][0]=descs.attr("href");
           data[count][1]=descs.attr("title");

to

           data[count][0]=e.attr("href");
           data[count][1]=e.attr("title");

and add as last line of the for loop: 并添加为for循环的最后一行:

if ( count == spaceCount )
   break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM