简体   繁体   中英

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? +strong text

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();
    }

  }

}

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.

Alternatively, you can do this:

for (Element link : links) {
    System.out.println(Jsoup.connect(link.absUrl("href")).get());
}

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