Solved

how to navigate website using Jsoup in java

Posted on 2013-06-11
6
1,590 Views
Last Modified: 2013-06-25
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
Comment
Question by:yescobar2012
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39238538
How will JSoup help you if some of the links are generated by javascript?
0
 

Author Comment

by:yescobar2012
ID: 39238847
But I have seen similar examples that does that using Jsoup, even though that source is in javascript
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39238944
JSoup supports javascript?? I didn't think so ...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:yescobar2012
ID: 39239285
If you can extract the links from the source you can use them.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39239454
I'm talking about javascript running in memory and therefore the DOM, not just links as text in the page
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39275455
:)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question