I want to create a search engine page where i can get google's number of result

for example: When I type the word "Car" it will search in the google's search engine and it will return the number of results.. Note: the page will not go to google's page

Google for 'google search api'. You would also require the help of some server-side language along with Javascript to achieve this task.

~s.o.s~ is, again, completely correct ... and you do need some server side language since JavaScript has what is known as a sandbox which keeps it from doing cross-site scripting, as a security model.

The following is an unorthodox approach and i would recommend following ~s.o.s~'s advice and research the official API.

But all the server-side script you need is this (PHP) -- so long as Google doesn't change it's layout format.

googit.php

<?php
// Results <b>1</b> - <b>10</b> of about <b>1,420,000,000</b> -- CURRNET GOOGLE FORMAT
$query = $_GET['q'];
$result = file_get_contents( "http://www.google.com/search?q=$query" );
$found = preg_match( '#Results <b>\d+</b> - <b>\d+</b> of about <b>([\d,]+)</b>#', $result, $match );
if ( $found ) {
  $count = str_replace( ',', '', $match[1] );
} else {
  $count = "'false'";
}
print "google( $count );";
?>

And to avoid the complexities of Ajax and iFrames this is the JavaScript to do the rest

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
  padding: 60px;
}
</style>
<script type="text/javascript">
window.onload = function () {
  document.getElementById('googit').onchange = function () {
    if ( script = document.getElementById('script') ) {
      script.parentNode.removeChild( script );
    }
    var head = document.getElementsByTagName('head').item(0);
    var script = document.createElement('script');
    script.setAttribute( 'id', 'script' );
    script.setAttribute( 'type', 'text/javascript' );
    script.setAttribute( 'src', 'googit.php?q=' + this.value );
    head.insertBefore( script, head.firstChild );
    
    document.getElementById('output').value = '... please wait ...';
  };
};

function google ( result ) {
  var output = document.getElementById('output');
  if ( result == 'false' ) {
    output.value = 'INVALID SEARCH';
  } else {
    output.value = result;
  }
}

</script>
</head>
<body>
  <label for="googit">Get Google Count</label><br />
  <label for="googit">Input: </label><input id="googit" type="text" />
  <input type="submit" value="Touch Me" /><br /><br />
  <label for="output">Result: </label><input id="output" type="text" />
</body>
</html>

Please forgive how lacking in style the above page is ... ;-)
I only worked this up for personal interest as a proof of concept

Ciao

Comments
cool

Okay, my proof of concept fails for multiple word searches ... :-(

But I believe these two changes fix that

// replace javascript line (IE wants this)
script.setAttribute( 'src', 'googit.php?q=' + this.value.replace( / /, '+' ) );

// add PHP line (FF wants this)
$query = $_GET['q'];
$query = str_replace( ' ', '+', $query );

I'm done now ... thanks for bearing with me.

Google don't take kindly to people running automated queries ( including scrapes ) without using their "official APIs", so you may find they block your server from communicating with the Google site if it makes alot of requests like this.

Which is hypocritic to say the least, since that's exactly how Google gathers its own information.

But anyway, certainly good stuff for a proof of concept.

Google don't take kindly to people running automated queries ( including scrapes ) without using their "official APIs", so you may find they block your server from communicating with the Google site if it makes alot of requests like this.

Which is hypocritic to say the least, since that's exactly how Google gathers its own information.

But anyway, certainly good stuff for a proof of concept.

Good reminder Matt -- google is strict.

Anyone trying to do SEO and trying to keep up on google's rules will make your head spin with all the restrictions they impose (admittedly, most are for the good of all though)

Cheers

This article has been dead for over six months. Start a new discussion instead.