简体   繁体   中英

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. 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). Can anyone explain how to make it work?

You are not using 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:

if ( count == spaceCount )
   break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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