Aimkiller Aimkiller - 1 year ago 99
HTML Question

[JAVA]Get html link from webpage

I want to get the link in this pic using java, image is below. There are few more links in that webpage. I found this code on stackoverflow, I don't understand how to use it though.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class weber{
public static void main(String[] args)throws Exception{
String url = "";
Document doc = Jsoup.connect(url).get();

/*String question ="#site-inner").text();
System.out.println("Question: " + question);*/

Elements anser ="#container .entry-title a");
for (Element anse : anser){
System.out.println("Answer: " + anse.text());

code is edited from the original I found tho. please help.


Answer Source

For your URL following code works fine.

public static void main(String[] args) {

    Document doc;
    try {

        // need http protocol
        doc = Jsoup.connect("").userAgent("Mozilla").get();
        // get page title
        String title = doc.title();
        System.out.println("title : " + title);

        // get all links (this is what you want)
        Elements links ="a[href]");
        for (Element link : links) {

            // get the value from href attribute
            System.out.println("\nlink : " + link.attr("href"));
            System.out.println("text : " + link.text());


    } catch (IOException e) {


output was

title : Ufology

link :
text : Shop

link :
text : Books

Following code filter the links by text of it.

        for (Element link : links) {

            if(link.text().contains("Arecibo Message"))//find the link with some texts
                System.out.println("here is the element you need");
                System.out.println("\nlink : " + link.attr("href"));
                System.out.println("text : " + link.text());


It’s recommended to specify a “userAgent” in Jsoup, to avoid HTTP 403 error messages.

Document doc = Jsoup.connect("").userAgent("Mozilla").get();

"Onna malli mage yuthukama kala."

refernce :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download