批量添加Yoast SEO的关键字和文章描述

批量添加Yoast SEO的关键字和文章描述

 

由于我是在后期使用这个wordpress的Yoast SEO插件的,导致之前的文章seo相应的关键字和文章描述都没有,不过从这个意义上说,这个插件做的很烂。

好了,不管怎么这个问题要解决

我们使用python脚本链接数据库,链接调用mysql语句更新添加

 

1
2
3
4

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","Fav!360","blog_360converter" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database.
sqlSearch = """select * from wp_postmeta where post_id=# and meta_key='_yoast_wpseo_focuskw';"""
sqlUpdate1 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_metadesc', post_title from wp_posts where id=#;"""
sqlUpdate2 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_focuskw', post_title from wp_posts where id=#;"""
sqlUpdate3 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_title', post_title from wp_posts where id=#;"""

sqlAllids = """select distinct(id) from wp_posts"""
cursor.execute(sqlAllids)
rows = cursor.fetchall()
for row in rows:
    try:
        # Execute the SQL command
        sqlSearch1 = sqlSearch.replace( "#", str(row[0]))
        print "search sql:", sqlSearch1
        cursor.execute(sqlSearch1)
        if cursor.rowcount == 0:
            print str(row[0]) + ' not found, now insert ———————————'
            sqlUpdate11 = sqlUpdate1.replace( "#", str(row[0]))
            sqlUpdate22 = sqlUpdate2.replace( "#", str(row[0]))
            sqlUpdate33 = sqlUpdate3.replace( "#", str(row[0]))
            print sqlUpdate11
            print sqlUpdate22
            print sqlUpdate33
            cursor.execute(sqlUpdate11)
            cursor.execute(sqlUpdate22)
            cursor.execute(sqlUpdate33)
        else:
            print cursor.rowcount
        # Commit your changes in the database
        db.commit()
    except:
        # Rollback in case there is any error
        db.rollback()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()

print "Database version : %s " % data

# disconnect from server
db.close()

 

 

 

 

 

如果你在使用上面这段代码的时候有什么问题,在下面留言,或者给我发邮件都可以。

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

留言

你的邮箱是保密的 必填的信息用*表示