Pesquisa Google Java

Alguem me consegue explicar porque é que não me retorna nada? penas diz que foi corrido com sucesso, mas sem qualquer retorno! Não vejo nada errado.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package googlesearchdemo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author filip
 */
public class GoogleSearchDemo {

  // pattern for extracting the link such as www.codeforeach.com/java/ ( domain
  // name + path )
  private static final Pattern p = Pattern
      .compile("([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/[^&]*)*");

  public static void main(String[] args) throws IOException {
    String searchQuery = "animais";
    List<String> links = searchGoogle(searchQuery);
    for (String link : links) {
      System.out.println(link);
    }

  }

  public static List<String> searchGoogle(String searchQuery) throws IOException {
    List<String> result = new ArrayList<>();
    // lets get the top results counting to nearly 15
    String request = "https://www.google.com/search?q=" + searchQuery + "&num=15";

      org.jsoup.nodes.Document doc = Jsoup.connect(request)
        .userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)").get();
    // get the required content from response . Here ( h3 a ) is the selector
    // pattern for selecting all heading links
    Elements links = doc.select("h3 a[href]");

    for (Element link : links) {
      String hrefValue = link.attr("href");
      if (hrefValue.startsWith("/url?q="))
        result.add(extractLink(hrefValue));
    }

    return result;
  }

  // extract required link from href value
  private static String extractLink(String href) {
    String result = null;
    Matcher m = p.matcher(href);

    if (m.find()) {
      result = m.group();
    }

return result;

Você já debugou o codigo ?

1 curtida

Debuguei e deu o seguinte erro:
Not able to submit breakpoint MethodBreakpoint [teste_email.Teste_email$1].getPasswordAuthentication '()Ljava/net/PasswordAuthentication;', reason: Breakpoint belongs to disabled source root 'C:\Users\filip\Documents\NetBeansProjects\teste_email\src'. See Window/Debugging/Sources.

Sabe como resolver?

Qual IDE vc está usando ?

Uso o NetBeans

qual versão o NetBeans e qual a versão Java está sendo compilado seu projeto ?

Utilizo no netbeans 8.2 e a versão do java é 1.8.0_191
Obrigado pela ajuda! :slight_smile:

Desculpa, somente posso dar uma olhada no seu codigo no final de semana, se tiver resolvido até lá posta aqui no forum como solucionado.

Ok! Agradeço a sua ajuda! Vou tentando!

Qual a versão do jsoup está usando ?

Utilizo a versão jsoup 1.12.1

blz, amanha eu vejo

1 curtida

Olha, verifiquei o programa e somente encontrei um erro minimo na parser da pagina da google. Bom, Eu tambem coloquei um decode pra ver se o resultado estava correto quando colocasse o endereço no navegador. Funcionou corretamente.

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class GoogleSearchDemo {

  // pattern for extracting the link such as www.codeforeach.com/java/ ( domain
  // name + path )
  private static final Pattern p = Pattern
      .compile("([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/[^&]*)*");

  public static void main(String[] args) throws IOException {
    String searchQuery = "animais";
    List<String> links = searchGoogle(searchQuery);
    for (String link : links) {
      System.out.println(link);
    }

  }

  public static List<String> searchGoogle(String searchQuery) throws IOException {
    List<String> result = new ArrayList<>();
    // lets get the top results counting to nearly 15
    String request = "https://www.google.com/search?q=" + searchQuery + "&num=15";
    
      org.jsoup.nodes.Document doc = Jsoup.connect(request)
        .userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)").get();
    // get the required content from response . Here ( h3 a ) is the selector
    // pattern for selecting all heading links
    
   //% System.out.println( "--> \n" + doc.toString() );    
   //% Elements links = doc.select("h3 a[href]");   
    
    Elements links = doc.select("a[href]");
  
    for (Element link : links) {
      String hrefValue = link.attr("href");
      
      if (hrefValue.startsWith("/url?q=")) {
          
         try {
            hrefValue = URLDecoder.decode(hrefValue, StandardCharsets.UTF_8.toString());
            
            result.add(extractLink(hrefValue));
            
         } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex.getCause());
         }
          
      }
    }

    return result;
  }

  // extract required link from href value
  private static String extractLink(String href) {
    String result = null;
    Matcher m = p.matcher(href);

    if (m.find()) {
      result = m.group();
    }

    return result;

  }
  
}

Bons Codigos

3 curtidas

Já funcionou! Muito obrigado pela ajuda, a sério! :slight_smile:

1 curtida

Mas já agora, pode-me explicar a lógica se eu quisesse buscar os nomes e não os links?! Seria mais fácil?

Bom, primeiramente você precisa ver dentro como está o html.

Pra fazer isso é bem simples,pode colocar um breakPoint atraves do debug na linha de parada e ver o que o jsoup esta retornando.

você verá internamente algo do tipo

<div class="kCrYT">
    <a href="/url?q=https://www.youtube.com/watch%3Fv%3D1D55PsWF7A4&amp;sa=U&amp;ved=2ahUKEwia-cPR55TmAhXbH7kGHfbmCHsQtwIwFHoECAMQAQ&amp;usg=AOvVaw3iFsYZd9AwNsX18oUV2YWk">
     <div class="BNeawe vvjwJb AP7Wnd">
      7 BATALHAS DE ANIMAIS GRAVADAS EM VÍDEO 4 - YouTube
     </div>
     <div class="BNeawe UPmit AP7Wnd">
      https://www.youtube.com › watch
     </div></a>
   </div>

Agora dê uma analisada na fonte e tambem na documentação da jsoup.
Vou postar o fonte com essa alteração pra você analisar melhor e fazer as alterações que te interessam.
E tambem destaco que existe outra forma de fazer isso, que é atraves da raspagem usando WebEngine e WebView, que podem ficar ocultos se necessário e usar uma função em javascript sendo chamada atraves da WebEngine.

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.util.Pair;
import javax.swing.text.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class GoogleSearchDemo {

  // pattern for extracting the link such as www.codeforeach.com/java/ ( domain
  // name + path )
  private static final Pattern p = Pattern
      .compile("([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/[^&]*)*");

  public static void main(String[] args) throws IOException {
    String searchQuery = "animais";
    List<Pair> links = searchGoogle(searchQuery);
    for (Pair link : links) {
     
      System.out.println("link=" + link.getKey() + "  url=" + link.getValue() );
    }
    
  }

  public static List<Pair> searchGoogle(String searchQuery) throws IOException {
    List<Pair> result = new ArrayList<>();
    
    // lets get the top results counting to nearly 15
    String request = "https://www.google.com/search?q=" + searchQuery + "&num=15";
     
      org.jsoup.nodes.Document doc = Jsoup.connect(request)        
        .userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)").get();
      
    // get the required content from response . Here ( h3 a ) is the selector
    // pattern for selecting all heading links
    
    //  System.out.println( "--> \n" + doc.toString() );
  
    Elements links = doc.select(".kCrYT");
    
    for (Element link : links) {
      Elements el_a = link.select("a");
     
      String hrefValue = el_a.attr("href");    
      Elements el_divs = el_a.select("div");
      
      String nome = "";
      if(el_divs.size() > 0) {
         nome = el_divs.get(0).html();
      }
       
      if (hrefValue.startsWith("/url?q=")) {
          
         try {
             
            String slink = extractLink(hrefValue);
            
            if( slink != null ) {             

               hrefValue = URLDecoder.decode(slink, StandardCharsets.UTF_8.toString());
            
               Pair pair = new Pair(nome, hrefValue );
            
               result.add( pair );
               
            }
             
         } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex.getCause());
         } catch(java.lang.IndexOutOfBoundsException ie) {
            ie.printStackTrace();
           // nao faca nada   
         }
          
      }
    }

    return result;
  }

  // extract required link from href value
  private static String extractLink(String href) {
    String result = null;
    Matcher m = p.matcher(href);

    if (m.find()) {
      result = m.group();
    }

    return result;

  }
  
}
1 curtida

É isso mesmo! Obrigado pela ajuda!

1 curtida

Agradeço muito a sua ajuda! Mas tenho mais uma duvida:
Como posso retirar a data da noticia ou do artigo publicado?
Isto:
há 2 horas

Obrigado :slight_smile:

Voce precisa fazer o scan de um bloco do html que tenha esses dados.

1 curtida

Obrigado :slight_smile:
Seria possivel fazer a pesquisa no site do Google News?