[wordpress主题]wordpress利用sitemap提高百度收录量

时间:2020-11-27  来源:WordPress  阅读:

Sitemap可方便网站管理员通知搜索引擎自己的网站上有哪些可供抓取的网页,对于提高网站的收录数据有很大的帮助,我们可以通过定时向搜索引擎提供自己的网站的实时页面地址来提高自己站点的收录率,百度提供了站长工具来帮助我们来优化网站对百度的友好度。
首先第一步登陆http://zhanzhang.baidu.com添加自己的网站域名并验证(需要添加一个cname到一个百度域名)
然后就可以向百度提交自己网站的链接来使baiduspider更智能的抓取自己的网站了,百度提供了三种方法给站长来提交自己的sitemap。
1,自动推送,通过在页面安插如下js代码,当页面被访问的时候,此页面就会被提交到百度。

<script>// (function(){ var bp = document.createElement("script"); bp.src = "//push.zhanzhang.baidu.com/push.js"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();
// ]]></script>
2,主动推送(实时),推送接口如下:

curl -H "Content-Type:text/plain" --data-binary @sitemap.txt "http://data.zz.baidu.com/urls?site=sixther.me&token=iYKCfM6pqMORqZrY&type=original"
//sitemap.txt为服务器上的sitemap文件路径,site参数为网站域名
可以通过crontab定时执行达到实时推送的目的,但这里有个问题,sitamap.txt也应该是实时更新的,这里我们可以通过查询wordpress的数据库来实时生成sitemap文件,具体请看后文。
3,sitemap方法,此方法通过在百度站长后台设置一个sitemap文件的url,然后百度会定时抓取该文件,sitemap文件支持三种格式:
♣ 文本格式:即一行一个url,这里跟第二种主动推送的文件格式一样,所以可以使用脚本定时生成这种格式的文件,然后两种方法都可以使用。
♣ xml格式,结构化的url集合,具体可查看sitemap文件格式
♣ sitemap索引格式,结构化的sitemap文件地址集合,可用来提交大量的sitemap文件里的url。
下面介绍一种从wordpress数据库生成txt格式的sitemap文件的方法,同理也可生成xml格式的,wordpress里发表的一切内容都存在wp_posts表里,可用如下sql查询到已经发表的文章名字:

select post_name from  wp_posts where post_status="publish"and post_type="post"
然后通过字符串拼接的方式可以生成所有文章的url,代码如下:

#!/usr/bin/env python
import MySQLdb

class WordPressDB:
        def __init__(self):
                self.conn=MySQLdb.connect(host="127.0.0.1",user="wordpress",passwd="******",db="wordpress",charset="utf8")
                self.cur=self.conn.cursor()
        def fetch_list(self,sql):
                self.cur.execute(sql)
                result=self.cur.fetchall()
                self.cur.close()
                return(result)
if __name__ == "__main__":
        sitemap_file_fd=open("sitemap.txt","w")
        wd=WordpressDB()
        query_post_sql="select post_name from  wp_posts where post_status="publish"and post_type="post""
        for i in wd.fetch_list(query_post_sql):
                post_url="https://sixther.me/" + i[0] + ".html" + "\n"
                sitemap_file_fd.write(post_url)
        sitemap_file_fd.close()
到的的结果如下:

https://sixther.me/monitor-haproxy-with-zabbix.html
https://sixther.me/generate-thumbnail-images-with-nginx.html
https://sixther.me/deploy-django-with-nginx.html
https://sixther.me/c-point-concept.html
https://sixther.me/vim-template-for-python-file.html
https://sixther.me/wordpress-category-page-404.html

这时,就可以使用主动推送或者sitemap的方法将这些url提交到百度,但前提是这些url必须是有效的

[wordpress主题]wordpress利用sitemap提高百度收录量

http://m.bbyears.com/wangyezhizuo/112968.html

推荐访问:wordpress教程 wordpress大学
相关阅读 猜你喜欢
本类排行 本类最新