1. 程式人生 > >怎麼讓百度知道你的網站(2)

怎麼讓百度知道你的網站(2)

前面說到,在百度“搜尋資源中心”提交了連結後還是收錄不了。
接下來我們要怎麼辦呢?
用一個簡單的比喻,百度就像是一個聲吶探測器,它探索著網路大海里的資源,越是龐大的它越容易發現,所以那些熱門網站就像一隻只鯨魚,他們不用怎麼樣百度就會發現他們,並且進行收錄,而我們的一些小網站就像小魚蝦米甚至浮游生物、海藻,要想百度找到我們很難,但是我們可以主動去找百度,讓它知道我們呀。
百度也給我們提供了這樣的機會:

(function(){
	var bp = document.createElement('script');
	var curProtocol = window.location.protocol.split(':')[0];
	//針對不同的協議型別匯入不同的指令碼
	bp.src = curProtocol === 'https'? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(bp, s);
})();

這是百度官方提供的一段js程式碼,只需要把程式碼放入中或者單獨寫個檔案匯入link都可以。
這段程式碼就是用來使我們主動的去“找”百度,讓它知道我們。
首先它新建一個指令碼區域:

	var bp = document.createElement('script');

然後再根據網站的協議不同匯入不同的指令碼:

	bp.src = curProtocol === 'https'? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(bp, s);

那 ‘https://zz.bdstatic.com/linksubmit/push.js’ 和 ‘http://push.zhanzhang.baidu.com/push.js’ 中的內容是什麼呢?
我在瀏覽器導航輸入連結:
https://zz.bdstatic.com/linksubmit/push.js

!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=window.location.href,t=document.referrer;if(!e.test(r)){var o="https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif";t?(o+="?r="+encodeURIComponent(document.referrer),r&&(o+="&l="+r)):r&&(o+="?l="+r);var i=new Image;i.src=o}}(window);

http://push.zhanzhang.baidu.com/push.js

!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=window.location.href,o=document.referrer;if(!e.test(r)){var n="//api.share.baidu.com/s.gif";o?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var t=new Image;t.src=n}}(window);

裡面是兩段指令碼,於是我想與其每次都要通過載入這兩段指令碼(而且兩段指令碼也有相似的地方),何不直接將這兩段指令碼和之前的程式碼一起寫到一個函式裡呢?
當然這件事情已經有人做好了,在百度搜索資源中心的工具解讀中就有這麼一段程式碼:


<script>

	(function(){

		var canonicalURL, curProtocol;

		//Get the <link> tag

		var x=document.getElementsByTagName("link");

		//Find the last canonical URL

		if(x.length > 0){

			for (i=0;i<x.length;i++){

				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){

					canonicalURL=x[i].href;

				}

			}

		}

		//Get protocol

	    if (!canonicalURL){

	    	curProtocol = window.location.protocol.split(':')[0];

	    }

	    else{

	    	curProtocol = canonicalURL.split(':')[0];

	    }

	    //Get current URL if the canonical URL does not exist

	    if (!canonicalURL) canonicalURL = window.location.href;

	    //Assign script content. Replace current URL with the canonical URL

    	!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();

</script>

詳情參考:
https://ziyuan.baidu.com/college/articleinfo?id=1604

我們只需要匯入這段程式碼到html中就好了。

可以參考一下我對這段程式碼的解讀:

(function(){
	var canonicalURL, curProtocol;
	//獲取頁面中所有的連結
	var x=document.getElementsByTagName("link");
	//找到所有的權威連結
	if(x.length > 0){
		for (i=0;i<x.length;i++){
		//<link rel="canonical" href="網頁權威連結"/>
		//需要注意的是link標籤裡的url連結是你所認為的規範、正確、希望百度收錄且參與排名的那個連結。
		//<link rel="canonical" href="網頁權威連結"/>放在head頭部
			if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
				canonicalURL=x[i].href;
			}
		}
	}
	//獲取協議型別:http/https, 如果cannonicalURL為空,預設是當下網頁的協議型別
	curProtocol = !canonicalURL ? window.location.protocol.split(':')[0] : curProtocol = canonicalURL.split(':')[0]

	//如果URL不存在,既是當下網頁的網址
	if (!canonicalURL) canonicalURL = window.location.href;
	//指定指令碼內容。用規範URL替換當前URL
	!function(){
		var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;
		if(!e.test(r)){
			var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";
			t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);
			var i=new Image;i.src=n}
	}(window);
})();

在每個html頁面都寫入這樣一段程式碼(或者寫到一個js檔案再匯入)之後,每當有人訪問網站,就會把網頁的連結都提交給百度了,相比之前只能通過百度搜索資源中心一個個的提交連結,現在無疑加快了許多速度!

注:要讓百度爬取的連結記得設定rel=“canonical”

訪問的人很少,收錄還是很慢,如何加快這一進度呢?歡迎繼續觀看博主系列文章。