Home > OS >  How can I specify amount of hyperlinks returned by jsoup?
How can I specify amount of hyperlinks returned by jsoup?

Time:10-15

I am working on a project, where I need to write a Java code to return hyperlinks from the web-page.

import org.jsoup.Jsoup;  
import org.jsoup.nodes.Document;  
import org.jsoup.nodes.Element;  
import org.jsoup.select.Elements;  
public class link_demo {  
     public static void main( String[] args ) throws IOException{  
            Document doc = Jsoup.connect("http://www.javatpoint.com").get();  
            Elements links = doc.select("a[href]");  
            for (Element link : links) {  

                    System.out.println("\nlink : "   link.attr("href"));  
                    System.out.println("text : "   link.text());  
            }       
     }  
}

This is the code that returns hyperlinks, but when I ran it, it returns all the links. I only need first 10 to be returned. Is there a way I can achieve that?

Thanks in advance.

CodePudding user response:

Elements implements Collection. So you can process it as a stream.

Document doc = Jsoup.connect("http://www.javatpoint.com").get();  
Elements links = doc.select("a[href]");  
links.stream()
    .limit(10)
    .forEach(link -> {  
        System.out.println("\nlink : "   link.attr("href"));  
        System.out.println("text : "   link.text());  
    });

CodePudding user response:

Why not using an index to store the count,and only get the first 10 records

public class link_demo {  
     public static void main( String[] args ) throws IOException{  
            Document doc = Jsoup.connect("http://www.javatpoint.com").get();  
            Elements links = doc.select("a[href]");
            int index = 0;          
            for (Element link : links) {  
                if(index >= 10){
                  break;
                }
                index  ;
                System.out.println("\nlink : "   link.attr("href"));  
                System.out.println("text : "   link.text());  
            }       
     }  
}
  • Related