google推https幾年了晌端,百度開(kāi)始宣傳全面https捅暴,但是,百度站內(nèi)搜索 自己的服務(wù)卻不走h(yuǎn)ttps咧纠,接口報(bào)錯(cuò)蓬痒。百度分享也是。
然后采用http://search.zhoulujun.cn/cse/search漆羔,用了7-8個(gè)月的樣子梧奢,還是繼續(xù),不聲不響地 改變了配置了演痒,突然就不通了——而且什么時(shí)候不通的都不知道
無(wú)賴亲轨,無(wú)奈,只有用google 自定義搜索引擎鸟顺,用api 接口弄好
用google search API配置google站內(nèi)搜索
谷歌搜索引擎定制化頁(yè)面地址
https://cse.google.com/cse/setup/basic?cx=009558619093950188775:b3wc7giu_hw
復(fù)制?搜索引擎 ID?惦蚊,保存
拖到最下面,找到?
自定義搜索 JSON API
每天的查詢數(shù)上限為 10000 次讯嫂。
直接打開(kāi)連接蹦锋,可以閱讀下內(nèi)容,了解下
在此https://developers.google.com/custom-search/v1/overview
頁(yè)面找到?API key欧芽,點(diǎn)擊 GET A KEY 按鈕莉掂,
復(fù)制key,保存
然后拼湊如下地址:
就是你的谷歌自定義站內(nèi)搜索引擎
谷歌的就完成了千扔,再來(lái)看下百度:
用百度站內(nèi)搜索api配置百度站內(nèi)搜索
登錄百度站內(nèi)頁(yè)面憎妙,點(diǎn)擊進(jìn)入API管理后臺(tái)
新建搜索引擎,點(diǎn)擊代碼示例
在代碼里面獲取如下代碼地址:http://zhannei.baidu.com/api/customsearch/apiaccept?sid=6351075239166713563&v=2.0&callback=init
直接下載曲楚,上傳自己服務(wù)厘唾,引用。
然后配置如下代碼
????var?cse?;????//參數(shù)為您的API引擎ID龙誊,已自動(dòng)填寫(xiě)抚垃,必需。
????var?form=document.querySelector('searchBaidu-bd');//關(guān)鍵詞輸入框
????var?textInput=document.getElementById('bdcsMain');//關(guān)鍵詞輸入框
????var?btnInput=document.getElementById("search");//搜索按鈕
????var?listBox=document.getElementById("list");//搜索結(jié)果列表框
????var?exchange=document.getElementById("exchange");//搜索結(jié)果列表框
????var?exchangeEngine=document.getElementById("exchangeEngine");//搜索結(jié)果列表框
????var?searchEngine=document.getElementById("searchEngine");//搜索結(jié)果列表框
????let?params=getUrlParams(window.location.search);
????let?initKeyword=params['q'];
????if(initKeyword){
????????initKeyword=decodeURIComponent(initKeyword);
????????textInput.value=initKeyword;
????}
????var?googleEnable=false;
????function?getUrlParams(str){
????????let?obj={};
????????str.replace(/([^&=?]+)=([^&=?]*)/g,function?(str,$1,$2)?{
????????????obj[$1]=$2;
????????????return?str;
????????});
????????return?obj;
????}
????function?searchBaidu?(text)?{
????????cse.getResult(text,?function?(data)?{
????????????console.info('百度搜索結(jié)果');
????????????console.log(data);
????????????createList(data,listBox)
????????});
????}
????function?init?()?{
????????cse=?new?BCse.Search("6351075239166713563");
????????initKeyword&&searchBaidu(initKeyword);
????}
????function?createList(arr,select){
????????var?str='';
????????if(arr&&arr.length){
????????????let?tempArr?=?arr.map(function?(data)?{
????????????????return?'?<li?class="article-li??">'?+
????????????????????'????????????????<a?class=""?href="'+data.linkUrl+'"?target="_blank">'+data.title+'</a>'?+
????????????????????'????????????????<time>'+data.dispTime+'</time>'?+
????????????????????'????????????</li>';
????????????});
????????????str=tempArr.join('');
????????}else?{
????????????str='<li>?沒(méi)有找到相關(guān)內(nèi)容——the?suitable?context?had?not?been?found?all?the?time.?</li>';
????}
????????select.innerHTML=str;
????????searchEngine.innerHTML='百度';
????????exchangeEngine.innerHTML='切換為谷歌搜索';
????}
????btnInput.addEventListener('click',function?(e)?{
????????e.preventDefault();
????????var?text=textInput.value;
????????if(googleEnable){
????????????text&&searchByGoogle(text);
????????}else?{
????????????text&&searchBaidu(text);
????????}
????});
????exchange.addEventListener('click',function?(e)?{
????????e.preventDefault();
????????window.location='?m=search&c=index&a=init&siteid=1&typeid=1&q='+textInput.value;
????});
????exchangeEngine.addEventListener('click',function?(e)?{
????????e.preventDefault();
????????var?text=textInput.value;
????????console.info('exchangeEngine.innerText');
????????console.log('__________');
????????if(exchangeEngine.innerText==='切換為百度搜索'){
????????????text&&searchBaidu(text);
????????????googleEnable=true;
????????????return?false;
????????}else?{
????????????text&&searchByGoogle(text);
????????????googleEnable=false;
????????????return?false;
????????}
????});
????function?hndlr(response)?{
????????//?console.log(JSON.stringify(response));
????????console.log(response);
????????var?str='';
????????if(response&&response.items&&response.items.length){
????????????let?tempArr?=?response.items.map(function?(data)?{
????????????????return?'?<li?class="article-li??">'?+
????????????????????'????????????????<a?class=""?href="'+data.link+'"?target="_blank">'+data.htmlTitle+'</a>'?+
????????????????????'????????????</li>';
????????????});
????????????str=tempArr.join('');
????????}else?{
????????????str='<li>沒(méi)有找到相關(guān)內(nèi)容</li>';
????????}
????????listBox.innerHTML=str;
????????searchEngine.innerHTML='谷歌';
????????exchangeEngine.innerHTML='切換為百度搜索';
????}
????function?searchByGoogle(keyword){
????????$.ajax({
????????????type:?'GET',
????????????url:?'https://www.googleapis.com/customsearch/v1',
????????????data:?{
????????????????q:?keyword,
????????????????key:'AIzaSyC_pehzzjm8_6lbsegGNgct1-LaACSpvkM',
????????????????cx:'009558619093950188775:b3wc7giu_hw',
????????????????siteSearch:?'zhoulujun.cn'
????????????},
????????????dataType:?'json',
????????????success:?function?(data)?{
????????????????console.info('google搜索結(jié)果');
????????????????console.log(data);
????????????????googleEnable=true;
????????????????hndlr(data)
????????????},
????????????error:function?(data)?{
????????????????hndlr();
????????????????//?let?timer=setTimeout(function?()?{
????????????????//?????searchBaidu(textInput.value);
????????????????//?????clearTimeout(timer);
????????????????//?},800)
????????????}
????????});
????}
????searchByGoogle(initKeyword);
????/*function?setBaidu?()?{
????????var??scriptEle=document.createElement('script');
????????scriptEle.src="{JS_PATH_CDN}andy/baidu.zn.js";
????????document.body.appendChild(scriptEle);
????}*/
具體代碼和效果可以參看:
https://www.zhoulujun.cn/index.php?m=content&c=index&a=lists&catid=204&q=%E6%93%8D
沒(méi)有做代碼優(yōu)化,只是抽點(diǎn)空把自己網(wǎng)站功能修復(fù)而已
按上面的操作的和應(yīng)該可以配置