How to get search links in google using javascript

Asked

Viewed 259 times

1

First of all, yes, I have checked the Apis provided by Google, such as Google Web Search (discontinued) and the new Google Custom Search, which from what I’ve seen works only for searches within a specified site. What I want is to get only the first links of a particular search (across the web), preferably if it were possible to do this in Javascript. Is there a way without having to use Apis, something using for example the google.com/search link? q=test and get the results? Grateful.

2 answers

1

Try this script I made (requires jQuery):

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function searchGoogle(Query) {
  // Seta A Variavel Final
  final = {}
  // Faz a Request Com o Cors-Anywhere Para Burlar A CORS-POLICY
  shweb = $.ajax({url: 'https://cors-anywhere.herokuapp.com/' + "https://www.google.com/search?q=" + Query, async: false})
  // Seta A Variavel Que Contem o HTML Da Pagina
  shhtm = stringToHTML(shweb.responseText)
  // Seta A Variavel Shres Com a Class 'LC20lb DKV0Md'
  shres = shhtm.getElementsByClassName('LC20lb DKV0Md')
  // Faz Um Loop Entre Os Links
  for (i = 0; i < shres.length - 1; i++) {
    // Seta O Final
    final[i] = {href: shres[i].parentElement.href, title: shres[i].innerText, desc: shres[i].parentElement.parentElement.parentElement.children.item(1).innerText}
  }
  // Retorna O Valor Final
  return final;
}

0

Speak Ablon, all right?

I’ve been researching here, apparently no API even to do this service.

Once I made one data scraping (html reading technique and extract information), but it’s a bit boring to do without using anything. You need to find the answer pattern of the page, i.e., the default way it assembles the elements and filters them through regular expressions.

Let me ask you, if you want to do this by where? By the browser itself or will do in some back-end?

  • All right. It can be said that by the back-end, I want to do a crawling of the obtained pages, but I want to get that page through the way I quoted in the question.

  • @Ablon I already managed to do a part. I’ll have to study a little more Regular Expression to help you in this, which in the end was an excellent doubt! I’ve already been able to find the Google creation pattern, now we need to extract the Urls and Title cohesively. As soon as you have the code I’ll put it here.

  • Okay, thank you! But how do you get Google to search within the code to get the result Urls?

  • @Ablon, what I do is HTTP request the google URL http://google.com/search and pass the parameter q with the search value, for example: http://google.com/search?q=maiores+cidades+do+mundo .

  • all right, I’m waiting, thanks in advance!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.