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

 1#!/usr/bin/python
 2#coding=utf-8
 3#Author by 361way.com
 4import time
 5import urllib3
 6import re
 7import MySQLdb
 8def  Gtag_ID(db,tags):
 9     tag_list = [100]
10     for tag in tags:
11       cursor = db.cursor()
12       id_qsql = "SELECT term_id FROM wp_terms where name=%s "
13       count = cursor.execute(id_qsql,tag)
14       if count == 0:
15            try:
16                sql = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
17                data = (tag,tag)
18                cursor.execute(sql,data)
19                db.commit()
20                tag_id = cursor.lastrowid
21                print 'Add the new tag %s ,tag_id is ' %(tag),type(tag_id)
22                tag_list.append(int(tag_id))
23                taxonomy_sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag','') " % (tag_id)
24                #tax_data = (tag_id,'post_tag','',0,0)
25                cursor.execute(taxonomy_sql)
26                db.commit()
27            except:
28                db.rollback()
29                db.close()
30       else:
31            id = cursor.fetchall()
32            for tag_id in range(count):
33               print "%s have been in wp_terms ,tag_id is  " %(tag),int(id[tag_id][0])
34               tag_list.append(int(id[tag_id][0]))
35               #print id[tag_id][0]
36     print tag_list
37db = MySQLdb.connect("localhost","root","123456","361way" )
38url = "http://segmentfault.com/blog/wangbinke/1190000000351425"
39now = time.time()
40pool = urllib3.PoolManager()
41r = pool.request('GET', url, assert_same_host=False)
42tags = re.findall(r'data-original-title=['"](.*?)['"]',r.data)
43print tags
44#tags = ['php','java','361way']
45Gtag_ID(db,tags)

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