Get search result in google with JQuery

4

Hello, I'm wondering how to do the following:
Using JQuery, I want to get the 1st result link from a Google search.

An example:

$("#bt_tempInfo").load("https://www.google.com.br/search?q=exemplo ._Rm:first-child", function(){
            link = "http://"+$("#bt_tempInfo").text();
        });

I just do not have permission to do on another page (of course). So I would like to know, one way to do this ... Is there any extension I can use from Google, or something similar?

    
asked by anonymous 15.07.2015 / 16:48

3 answers

1

Since you can not use Google Custom Search as suggested by Sergio , you can risk Google Web Search API , but remember that deprecated since November 2010.

In this case, you can load the API on your page, hide the element with the search, set the text to be searched and wait for the return.

In the example below, I mounted an Object that loads Google Web Search API , it fires an event ( onInitComplte ) when Search is available and another event ( onSearchComplete ) when the query is complete. >

In the example below, it logs onto the console the sponsored link and the first link.

var Busca = function (element) {
  this.container = element;
  this.initialize();
}

Busca.prototype.buscar = function (texto) {
  var self = this;
  var click = new Event("click");
  
  self.control.prefillQuery(texto);
  //self.input.value = texto;
  self.button.dispatchEvent(click);
}

Busca.prototype.initialize = function () {
  var self = this;
  google.load("search", "1");  
  google.setOnLoadCallback(function () {  
    var search = {};
    self.control = new google.search.SearchControl();
    self.control.addSearcher(new google.search.WebSearch());
    self.control.addSearcher(new google.search.NewsSearch());
    self.control.draw(self.container);
    self.input = self.container.querySelector("input.gsc-input");
    self.button = self.container.querySelector("input.gsc-search-button");
    self.result = self.container.querySelector("div.gsc-results-wrapper-nooverlay");
    
    self.control.setSearchCompleteCallback(this, function (event) {
      console.log(event);
      var results = {};
      results.container = self.result.querySelector("div.gsc-results");
      results.primeiro = {};
      results.primeiro.container = results.container.querySelector("div.gsc-result");
      results.primeiro.titulo = results.primeiro.container.querySelector("a.gs-title");

      var expansao = {};

      expansao = {};
      expansao.container = results.container.querySelector("div.gsc-expansionArea");
      expansao.primeiro = {};
      expansao.primeiro.container = expansao.container.querySelector("div.gsc-result");
      expansao.primeiro.titulo = expansao.primeiro.container.querySelector("a.gs-title");
      
      results.expansao = expansao;
      if (self.onSearchComplete) {
        self.onSearchComplete(event.hf, results);
      }
    });
    self.onInitComplete();
  });
}

var container = document.getElementById("searchcontrol");
var search = new Busca(container);
search.onInitComplete = function () {
  search.buscar("Hello Wolrd");
}
search.onSearchComplete = function (texto, results) {
  console.log([
    texto, 
    results.primeiro.titulo.href, 
    results.expansao.primeiro.titulo.href
  ]);
}
<script src="https://www.google.com/jsapi"type="text/javascript"></script>
<div id="searchcontrol"></div>
    
04.02.2016 / 15:20
0

Using only jQuery, I do not know if it has any ... but I think you can do it in PHP.

<?php
 // Endereço do site
 $url = 'http://www.site.com.br';

 // Pegando dados do Site e colocando em uma String
 $dadosSite = file_get_contents($url);

 // Exibindo o retorno
 echo $dadosSite;
?>

In case you adapt, I think you'll be able to do what you want.

References:

link

link

    
15.07.2015 / 17:06
0

Friend, try with ajax

function googleSearch(settings){

    var apiURL = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0';

    $.getJSON(apiURL,{
        q   : 'boschini busca',
    },function(r){
        console.info('Data:',r);
    });
}

googleSearch()
    
01.12.2015 / 13:15