Reputation: 45
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();
}
}
}
Upvotes: 2
Views: 615
Reputation: 43013
Alternatively, you can do this:
for (Element link : links) {
System.out.println(Jsoup.connect(link.absUrl("href")).get());
}
Upvotes: 0
Reputation: 1080
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.
Upvotes: 1