Link to home
Start Free TrialLog in
Avatar of yescobar2012
yescobar2012

asked on

how to navigate website using Jsoup in java

I need help I am learning Jsoup and I need to know how can i navigate in Jsoup to a different link, for this example I have done the basic get the title, get links and get texts. But I want to be able to use one of those child links and go to the inside that child link. For example from google web page I want to be able to go to youtube page because its one of the child links in google and once in youtube pick another child link and than be able to grab a string. How would I be able to do this in Jsoup? Thanks in advance!

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class JSoupTest {


public static void main(String args[]) {

    try {


        Document doc=Jsoup.connect("http://www.google.com").get();

        // get page title
        String title = doc.title();
        System.out.println(title);

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

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

        }

        for( Element element : doc.select("p") )    
                    // Select all 'p'-Tags and loop over them
        {
            if( element.hasText() )                 
                    // Check if the element has text (since there are some empty too)
            {
              System.out.println(element.text()); // print the element's text
            }
        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

How will JSoup help you if some of the links are generated by javascript?
Avatar of yescobar2012
yescobar2012

ASKER

But I have seen similar examples that does that using Jsoup, even though that source is in javascript
JSoup supports javascript?? I didn't think so ...
If you can extract the links from the source you can use them.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)