0

I want to print the text inside <span> tag which is inside <a> tag. I want to print 37 which is inside <span class="rep-score">37</span>

<a href="//stackoverflow.com"
       class="site-link js-gps-track"
       data-id="1"
       data-gps-track="
            site.switch({ target_site:1, item_type:3 }),
        site_switcher.click({ item_type:1 })">
        <div class="site-icon favicon favicon-stackoverflow" title="Stack Overflow"></div>
        Stack Overflow
            <span class="rep-score">37</span>
</a>

Below is the code which I wrote to do this but nothing gets printed.
Can somebody explain why it's not working.

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

import org.jsoup.nodes.Document; 
class Repoo
{ 
    static int count=0;
    // String html;
    public static void main(String s[])throws IOException
    {
        try{
    // Document doc=Jsoup.connect("http://www.javatpoint.com/java-tutorial").get();
    // Document doc=Jsoup.connect("http://stackoverflow.com/").get();
    Document doc = Jsoup
    .connect("http://www.stackoverflow.com")
    .userAgent("Google Chrome/48.0.2564.116 m")
    .get();

    // System.out.println("doc");
    // Elements link=(Elements)doc.select("span[class]");
    // Elements select=doc.select(".site-icon favicon favicon-stackoverflow");
    Elements select=doc.select("a.site-link js-gps-track > span.rep-score");

    // Elements link=(Elements)doc.select("div");

    // Elements link = doc.select("span").first();
    // Elements link = (Elements)doc.select("span");
     // Elements link = (Elements)doc.select("a[href]");

for(Element el: select)
{
    // System.out.print("-");
    // String repo=el.attr("class");
    System.out.println(el.text());
    // System.out.println(el.ownText());




//  if(repo.equals("rep-score"))
//  {
//   System.out.println(el.attr("class"));  
//  System.out.println(el.text());
// }
    // System.out.println(el.attr("id"));
    // count++;

    // String str=el.attr("href");
    // System.out.println(str);

}
// System.out.println("<"+count+">");
}catch(IOException e){System.out.println(e);}
}
}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
asad_hussain
  • 1,959
  • 1
  • 17
  • 27

1 Answers1

2

Your code didn't send any credentials required to login to Stack Overflow so you are getting as response page for unregistered user which doesn't contain any <span class="rep-score">37</span> tag.

You could try

BTW if you want to select <a ..> with few classes simply combine them with a.class1.class2, not a.class1 class2 because such selector will try to find a.class1 and then <class2 ..> tag in it.

So if you will able to login via jsoup and obtain doc which really will contain that span you should be able to select it with

Elements select=doc.select("a.site-link.js-gps-track > span.rep-score");
//                                     ^-we combine few classes with `.`
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • @a874 What do you mean by "I am already logged into the website"? If by that you mean that you are logged in your browser then try use other browser and log in on different account. Which value should jsoup return then? Answer is "neither" because information about started sessions should belong only to browser and not be shared. Try to think of Jsoup as separate browser (very simplified because it can't even execute JavaScript). – Pshemo Mar 06 '16 at 19:15
  • I am unable to understand.Can you just give the snippet of how to access the text of tag which is inside tag ? – asad_hussain Mar 06 '16 at 20:39
  • 1
    If you ware able to obtain response with jsoup which really contains `37` then you should be able to select it via `.select("a.site-link.js-gps-track > span.rep-score")`. If that doesn't work than your `doc` most probably doesn't contain any `37` because you ware not able to log in via jsoup to Stack Overflow. – Pshemo Mar 06 '16 at 20:57