前面说到,在百度“搜索资源中心”提交了链接后还是收录不了。
接下来我们要怎么办呢?
用一个简单的比喻,百度就像是一个声呐探测器,它探索着网络大海里的资源,越是庞大的它越容易发现,所以那些热门网站就像一只只鲸鱼,他们不用怎么样百度就会发现他们,并且进行收录,而我们的一些小网站就像小鱼虾米甚至浮游生物、海藻,要想百度找到我们很难,但是我们可以主动去找百度,让它知道我们呀。
百度也给我们提供了这样的机会:
(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”
访问的人很少,收录还是很慢,如何加快这一进度呢?欢迎继续观看博主系列文章。