Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1820
  • Last Modified:

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

0
yescobar2012
Asked:
yescobar2012
  • 4
  • 2
1 Solution
 
CEHJCommented:
How will JSoup help you if some of the links are generated by javascript?
0
 
yescobar2012Author Commented:
But I have seen similar examples that does that using Jsoup, even though that source is in javascript
0
 
CEHJCommented:
JSoup supports javascript?? I didn't think so ...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yescobar2012Author Commented:
If you can extract the links from the source you can use them.
0
 
CEHJCommented:
I'm talking about javascript running in memory and therefore the DOM, not just links as text in the page
0
 
CEHJCommented:
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now