How to get the number of results from a Google query

While trying to develop a tool to eliminate keywords from my own problem, I had a problem getting the total number of Google query results. To make this as clear as possible here, the screen of the number I'm looking to get: enter image description here

I could not find any links on how to do this in the Google Custom Search API, so I created a small scraper, but I believe that this is not the best way to do this. Here is my code:

<?php $url = "http://www.google.com/search?q=".$keyword; $text = file_get_contents($url); //get string between 2 strings function function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } //fetch the string between "About" and "results" $res = get_string_between($text, "About", "results"); //keep only numeric characters $res = preg_replace('/[^0-9]+/', '', $res); ?> 

Can you suggest a better way to do this? (Preferably using the Google API)

+4
source share
1 answer

Use regex:

 $params = array('q' => 'shark with lasers that shoot monkeys with balloons on skyscrapers please give me less results asdafasddfg'); $content = file_get_contents('http://www.google.com/search?' . http_build_query($params)); preg_match('/About (.*) results/i', $content, $matches); echo !empty($matches[1]) ? $matches[1] : 0; // output: 14,300 

I can not find anything in my APIs. Beware, this is against Google TOS. Bing API will give you the result.

+1
source

Source: https://habr.com/ru/post/1437879/


All Articles