Get span text from a site and return it to the console

-1

How can I get a span of a website and return the text in the console? I tried this way, but I do not know how to get the Span:

public void SpanSite(){
   URL url = new URL("https://google.com.br");
   BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
}

I accept other methods

    
asked by anonymous 02.12.2017 / 19:39

1 answer

0

You can use the jsoup library for this. Example: To get all spans from google , you could do this:

Document doc = Jsoup.connect("https://www.google.com.br").get();
doc.select("span")
    .stream()
    .forEach(s -> System.out.println(s.text()));

Edit :

To search for an element that contains a given text, you can use the Pseudo-selectors :containsOwn(textoASerBuscado) or :matchesOwn(regex) . Example:

Document doc = Jsoup.connect("https://www.google.com.br").get();
doc.select("span:containsOwn(google)")   //Busca todos os spans que contenham "google"
    .stream()
    .forEach(s -> System.out.println(s.text()));

To find out about other selectors, you can look in the Selector class documentation

    
02.12.2017 / 20:38