javajsoup

How to download content from URL using Jsoup and Java


I have this code which extracts all links from an URL using JSOUP. I need to put all the content from these links into a txt. How can I do this using Java?

public class Main {
  public static void main(String[] args) {
    Document doc, content;
    try {
        doc = Jsoup.connect("http://fmi.unibuc.ro/ro").get();
        System.out.print(doc);

        Elements links = doc.select("a[href]");
        for (Element link : links) {

            System.out.println("\nlink : " + link.attr("href"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

Solution

  • You need to extract the URL from the links like this:

    for (Element link : links) 
        System.out.println(Jsoup.connect(link.baseUri()).get());
    

    It will print the content of all links on the console.