ประการแรกสิ่งนี้ไม่ซ้ำกับ: เหตุใดการค้นหาของ Google ผ่าน Google API จึงส่งผลลัพธ์ที่แตกต่างให้กับการค้นหาในหน้าเว็บ หรือคำถาม "Google Search API อื่น ๆ ที่ส่งคืนผลลัพธ์ที่ไม่คาดคิด" ใด ๆ ที่ฉันสามารถทำได้ หา.
ฉันกำลังเขียนโปรแกรมจาวาสคริปต์ขนาดเล็กซึ่งใช้ Google Websearch API ฉันกำลังสร้างลิงก์แบบกำหนดเองสำหรับหน้าสถานที่ซื้อของฉัน ซึ่งควรจะนำผู้ใช้ไปยังหน้าที่ถูกต้องบนไซต์ตัวแทนจำหน่ายของเราเพื่อซื้อผลิตภัณฑ์ที่พวกเขาดูครั้งล่าสุดเมื่อคลิก "สถานที่ซื้อ" ฉันใช้วิธีการจาวาสคริปต์ของ document.referrer จากนั้นแยกวิเคราะห์ URL เพื่อพิจารณาว่าหน้าใดในไซต์ของฉันที่ผู้ใช้เข้าชมครั้งล่าสุด
จากนั้น ฉันสร้างสตริงการสืบค้นที่เหมาะสม จำกัดไซต์ไว้เฉพาะตัวแทนจำหน่ายรายใดรายหนึ่ง บอก Google ให้ส่งคืนผลลัพธ์เพียงรายการเดียว จากนั้นจึงเรียกใช้การสืบค้น จากนั้นฉันใช้ URL เพื่อเขียน HTML ใหม่ใน div href นั้นไปยังผลการค้นหาแรก (และเท่านั้น) โดยพื้นฐานแล้วฉันกำลังเลียนแบบฟังก์ชั่นของปุ่ม "ฉันรู้สึกโชคดี" ที่ Google เคยมี แต่จัดโครงสร้างข้อความค้นหาในลักษณะที่ฉันได้ผลลัพธ์ที่ต้องการอย่างแน่นอน
โอเค เมื่อคุณรู้แล้วว่าฉันกำลังทำอะไรอยู่และทำไมฉันถึงทำอย่างนั้น ต่อไปนี้คือสิ่งที่แปลกไป: ผลลัพธ์ส่วนใหญ่ที่ฉันได้ทำงานได้อย่างสมบูรณ์แบบ พวกเขาส่งคืนผลการค้นหาแรก บางส่วน (และเหมือนกันเสมอ) ส่งคืนผลการค้นหาที่สอง
นี่คือโค้ดที่คุณสามารถคัดลอกและวางลงใน Code Playground ก> ตัวคุณเองเพื่อดูปัญหาที่ฉันอ้างอิงด้วย:
<!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="/th'+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>
ฉันได้รวม URL หน้าผลิตภัณฑ์ตัวอย่างแทนที่จะใช้วิธีการ document.referrer (เนื่องจากเห็นได้ชัดว่าคุณไม่ได้สำรวจเว็บไซต์ของฉันในขณะนี้) เพื่อสาธิตสิ่งที่เกิดขึ้น ฉันยังได้สร้างแท็ก div พิเศษที่ด้านบนซึ่งแสดงว่ากำลังค้นหาอะไรอยู่
สังเกตว่าอันสุดท้ายในรายการ (vintage king หรือที่สี่ถึงสุดท้ายในโซลูชัน proaudio) ดึง URL สำหรับผลการค้นหา SECOND ลงมา หากคุณค้นหาแบบเดียวกันทั้งหมดด้วยตัวเอง อย่างไรก็ตาม ส่วนใหญ่ดึงผลการค้นหา FIRST ได้อย่างถูกต้อง (เช่น sweetwater, b&h, full Compass) สิ่งนี้เกิดขึ้นกับการค้นหาเกือบทุกครั้งที่ฉันทำบนเว็บไซต์เหล่านั้น ฉันรู้ว่าสิ่งนี้ไม่เกี่ยวข้องกับสิ่งใดที่เกี่ยวข้องกับเบราว์เซอร์ของฉัน และการตั้งค่าที่เก็บไว้ของฉันตั้งแต่ฉันได้ลองใช้โหมดไม่ระบุตัวตนนี้ บนคอมพิวเตอร์เครื่องอื่น ในเบราว์เซอร์ที่แตกต่างกัน ฯลฯ มีใครทราบบ้างว่าเกิดอะไรขึ้นที่นี่ เหตุใดฉันจึงไม่สามารถรับผลการค้นหาแรกจากการค้นหาทางเว็บของ Google ให้เป็น URL ที่ส่งคืนได้เสมอไป