一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2014-12-18 编辑:简简单单 来源:一聚教程网

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

 代码如下 复制代码

#!/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值打印出来。

热门栏目