Aimkiller Aimkiller - 2 months ago 8
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;
import org.jsoup.select.Elements;

public class weber{
public static void main(String[] args)throws Exception{
String url = "http://www.skyovnis.com/category/ufology/";
Document doc = Jsoup.connect(url).get();

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

Elements anser = doc.select("#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.

image

Answer

For your URL following code works fine.

public static void main(String[] args) {

    Document doc;
    try {

        // need http protocol
        doc = Jsoup.connect("http://www.skyovnis.com/category/ufology/").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 = doc.select("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) {
        e.printStackTrace();
    }

  }

output was

title : Ufology

link : http://www.shop.skyovnis.com/
text : Shop

link : http://www.shop.skyovnis.com/product-category/books/
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("http://anyurl.com").userAgent("Mozilla").get();

"Onna malli mage yuthukama kala."

refernce :

https://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/