python获取和自动插入wordpress tags标签

2014年12月17日 发表评论 阅读评论

本代码纯粹是为了测试练习之用,费话不多说,直接上代码:

#!/usr/bin/python
#coding=utf-8
#Author by 361way.com
import time
import urllib3
import re
import MySQLdb
def  Gtag_ID(db,tags):
     tag_list = [100]
     for tag in tags:
       cursor = db.cursor()
       id_qsql = "SELECT term_id FROM wp_terms where name=%s "
       count = cursor.execute(id_qsql,tag)
       if count == 0:
            try:
                sql = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
                data = (tag,tag)
                cursor.execute(sql,data)
                db.commit()
                tag_id = cursor.lastrowid
                print 'Add the new tag %s ,tag_id is ' %(tag),type(tag_id)
                tag_list.append(int(tag_id))
                taxonomy_sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag','') " % (tag_id)
                #tax_data = (tag_id,'post_tag','',0,0)
                cursor.execute(taxonomy_sql)
                db.commit()
            except:
                db.rollback()
                db.close()
       else:
            id = cursor.fetchall()
            for tag_id in range(count):
               print "%s have been in wp_terms ,tag_id is  " %(tag),int(id[tag_id][0])
               tag_list.append(int(id[tag_id][0]))
               #print id[tag_id][0]
     print tag_list
db = MySQLdb.connect("localhost","root","123456","361way" )
url = "http://segmentfault.com/blog/wangbinke/1190000000351425"
now = time.time()
pool = urllib3.PoolManager()
r = pool.request('GET', url, assert_same_host=False)
tags = re.findall(r'data-original-title=['"](.*?)['"]',r.data)
print tags
#tags = ['php','java','361way']
Gtag_ID(db,tags)

实现的功能是,如果wordpress中存在该tag,取出该tags的tag id值(term_id);如果不存在,插入tag值,并将插入后的id值打印出来。




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.