The Google search API sometimes returns the first result, sometimes the second result compared to the web interface

Firstly, this is not a duplicate: Why does google search on google api return different search results on a web page? None of the others I could find, the "google search API returns unexpected results."

I am writing a small javascript program that uses the Google web search API. I create custom links for mine where to buy a page that should direct the user to the correct page on our dealers website in order to buy the product that they last looked at when they clicked "where to buy". I use the javascript method document.referrer , then parse the url to determine which page the last visitor visited on my site.

Then I create the appropriate query string, restricting the site to a specific dealer, tell Google to return only one result, and then execute the query. Then I use the URL to rewrite the HTML in this particular href div for the first (and only) search result. I essentially imitate the functions of the β€œI feel happy” button that Google used, but structuring the request in such a way as to get exactly the result I'm looking for.

OK, so now that you know what I am doing and why I am doing it, this is where things are strange: most of the results I get are excellent. They return the first search result. Some of them (and always the same ones) return a second search result.

Here is the code that you can copy and paste into the Code Playground yourself to see the question, which I mean too:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Google AJAX Search API Sample</title> <script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1'); var SearchVariables = [ ["alto","altomusic.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/altomusic-button.png","wp-image-6343"], ["acme","www.acmelectronics.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/acme-button.png","wp-image-6344"], ["bhphoto","www.bhphotovideo.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/bh-button3.png","wp-image-6345"], ["frontend","www.frontendaudio.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/frontend-button.png","wp-image-6346"], ["fullcompass","www.fullcompass.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/fullcompass-button3.png","wp-image-6347"], ["performance","www.performanceaudio.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/performance-audio-button.png","wp-image-6348"], ["proaudio","www.proaudiosuperstore.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/ProAudioSolutions-button.png","wp-image-6349"], ["rmc","www.rmcaudiodirect.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/rmc-button1.png","wp-image-6350"], ["sweetwater","www.sweetwater.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/sweetwater-button3.png","wp-image-6351"], ["vintageking","www.vintageking.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/vintageking-button3.png","wp-image-6352"] ]; var Search = new Array(SearchVariables.length); function searchComplete() { for(var i = 0; i < Search.length; i++){ if (Search[i].results && Search[i].results.length > 0) { var contentDiv = document.getElementById(Search[i].ed[0]); var curURL = Search[i].results[0].unescapedUrl; var newImage = ' <a href="'+curURL+'"><img class="alignleft size-full '+Search[i].ed[2]+'" title="'+Search[i].ed[0]+'" src="'+Search[i].ed[1]+'" alt=" " width="235" height="94" /></a>'; contentDiv.innerHTML = newImage; } } } function parseURLString(inString) { var OutString = 'earthworks'; inString = inString.replace(/-/g,' '); inString = inString.replace('zdt',''); var MicVsPre=['preamps','microphones']; var idx = []; for(var j=0;j<MicVsPre.length;j++) { if(inString.search(MicVsPre[j])>=0) { OutString = OutString+' '+MicVsPre[j]; for(var i=inString.length;i>0;i--) { if(inString[i]=='/') { idx.push(i); } } OutString = OutString+' '+inString.substring(idx[0],idx[1]+1); } } return OutString; } function OnLoad() { var String_To_Parse = 'http://www.earthworksaudio.com/microphones/qtc-series-2/qtc40/'; var query = parseURLString(String_To_Parse); var testloc = document.getElementById('vardiv'); testloc.innerHTML = query; for(var i = 0; i < SearchVariables.length; i++) { Search[i] = new google.search.WebSearch(); Search[i].setNoHtmlGeneration(); Search[i].setResultSetSize(1); Search[i].setUserDefinedLabel([SearchVariables[i][0],SearchVariables[i][2],SearchVariables[i][3]]); Search[i].setSiteRestriction(SearchVariables[i][1]); Search[i].setSearchCompleteCallback(this, searchComplete,null); Search[i].execute(query); } } google.setOnLoadCallback(OnLoad); </script> </head> <body style="font-family: Arial;border: 0 none;"> <div id="vardiv">Testing</div> <div id='acme'></div> <div id='alto'></div> <div id='bhphoto'></div> <div id='fullcompass'></div> <div id='frontend'></div> <div id='performance'></div> <div id='proaudio'></div> <div id='rmc'></div> <div id='sweetwater'></div> <div id='vintageking'></div> </body> </html>​ 

I included a sample product page URL instead of actually using the document.referrer method (since obviously you are not viewing my site right now) to demonstrate what is happening. I also created an extra div tag at the top, which shows what kind of search is being performed.

Note how the last one from the list (the old king, or the fourth to hold out in proaudio soltuions) is how to pull out the URL for the SECOND search result if you do the same search yourself. However, most of them correctly pull out the FIRST search result (for example: sweetwater, b & h, full compass). This happens with almost every search I do on these specific sites. I know that this has nothing to do with my browser and my saved settings, since I tried it incognito on other computers, in different browsers, etc. Does anyone know what is going on here? Why can not I always get the first search result from google web search to be returned URL?

+6
source share
1 answer

I had this problem when I was doing something similar, and according to what I found then (iirc), the returned results depend on the language settings that you pass to google and the browser id string.

Please note that this could change, and this is a long shot, but it is worth checking out.

EDIT:

I just saw that you are looking for some kind of evidence for an answer, so here you go: http://www.windmeadow.com/node/36 <- Google provides various browser-based search results. http://productforums.google.com/forum/#!topic/webmasters/7K02-AUCTVM <- Different search results with different languages

EDIT 2:

This is a bug in the AJAX Googles API: http://code.google.com/p/google-ajax-apis/issues/detail?id=43

+2
source

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


All Articles