Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与web界面相比,Google搜索API有时会重新获得第一个结果,有时会重新获得第二个结果_Javascript_Api_Search - Fatal编程技术网

Javascript 与web界面相比,Google搜索API有时会重新获得第一个结果,有时会重新获得第二个结果

Javascript 与web界面相比,Google搜索API有时会重新获得第一个结果,有时会重新获得第二个结果,javascript,api,search,Javascript,Api,Search,首先,这不是重复的:也不是任何其他“谷歌搜索API返回意外结果”的问题,我已经能够找到 我正在编写一个使用GoogleWebSearchAPI的小javascript程序。我正在为我的where to buy页面创建自定义链接,该链接应将用户引导到经销商网站上的正确页面,以购买他们上次单击“where to buy”时查看的产品。我使用document.referer的javascript方法,然后解析URL以确定用户上次访问我网站上的哪个页面 然后我创建一个合适的查询字符串,将站点限制在特定的

首先,这不是重复的:也不是任何其他“谷歌搜索API返回意外结果”的问题,我已经能够找到

我正在编写一个使用GoogleWebSearchAPI的小javascript程序。我正在为我的where to buy页面创建自定义链接,该链接应将用户引导到经销商网站上的正确页面,以购买他们上次单击“where to buy”时查看的产品。我使用document.referer的javascript方法,然后解析URL以确定用户上次访问我网站上的哪个页面

然后我创建一个合适的查询字符串,将站点限制在特定的经销商,告诉google只返回一个结果,然后运行查询。然后,我使用URL将特定div href中的HTML重写为第一个(也是唯一一个)搜索结果。我基本上是在模仿谷歌曾经拥有的“我感觉很幸运”按钮的功能,但是以这样一种方式构造查询,我可以得到我想要的结果

好了,现在你知道我在做什么,为什么我要这么做了,这里是事情变得奇怪的地方:我得到的大多数结果都是完美的。它们返回第一个搜索结果。其中一些(总是相同的)返回第二个搜索结果

以下是您可以复制并粘贴到中的代码,以查看我所指的问题:

<!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>​ 

GoogleAjax搜索API示例
load('search','1');
变量变量=[
[“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”],
[“前端”,“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”],
[“性能”,“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”],
[“甜水”,“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=新数组(SearchVariables.length);
函数searchComplete(){
for(var i=0;i0){
var contentDiv=document.getElementById(搜索[i].ed[0]);
var curURL=Search[i]。结果[0]。unescapedUrl;
var newImage='';
contentDiv.innerHTML=newImage;
}
}     
} 
函数parseURLString(inString)
{
var OutString=‘土方工程’;
安装=安装。更换(/-/g');
安装=安装更换('zdt','');
var MicVsPre=['前置放大器','麦克风'];
var idx=[];
对于(变量j=0;j=0)
{
OutString=OutString+''+MicVsPre[j];
对于(变量i=inString.length;i>0;i--)
{
如果(指令[i]=='/')
{
idx.push(i);
}
}
OutString=OutString+''+inString.substring(idx[0],idx[1]+1);
}
}
回报突出;
}
函数OnLoad(){
变量字符串\u到\uhttp://www.earthworksaudio.com/microphones/qtc-series-2/qtc40/';     
var query=parseURLString(字符串到解析);
var testloc=document.getElementById('vardiv');
testloc.innerHTML=查询;
对于(var i=0;i
我加入了一个示例产品页面URL,而不是实际使用document.referer方法(因为显然您现在没有浏览我的网站)来演示发生了什么。我还在顶部创建了一个额外的div标记,显示正在执行的确切搜索


请注意,如果您自己进行完全相同的搜索,列表中的最后一个(vintage king,或proaudio soltuions中倒数第四个)都会下拉第二个搜索结果的URL。然而,他们中的大多数人都正确地下拉了第一个搜索结果(例如:sweetwater、b&h、full compass)。这几乎发生在我在这些特定网站上进行的每一次搜索中。我知道这与我的浏览器和我的存储设置无关,因为我曾在其他计算机上、不同的浏览器中尝试过这种方法。有人知道这里发生了什么吗?为什么我不能总是从基于web的Google搜索中获得第一个搜索结果作为返回的URL?

当我