Reputation: 2001
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);}
}
}
Upvotes: 0
Views: 1242
Reputation: 124215
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 `.`
Upvotes: 2