python查询wordpress数据库_python3下的一个类-直接用数据库发布WordPress博文

class wordpress_post:

def __init__(self,tittle,content):

self.tittle=tittle

self.content=content

def mysql_con(self):

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='pwd', db='wordpress', charset='utf8') #将这里换为你的数据库地址

return conn

def up(self):

times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

sql="INSERT INTO wp_posts(post_author,post_date,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_name,to_ping,pinged,post_modified,post_content_filtered,post_parent,menu_order,post_type,comment_count) VALUES ('1','%s','%s','%s','','publish','open','open','%s','','','%s','','0','0','post','0')" % (str(times),str(self.content),str(self.tittle),str(self.tittle),str(times))

return sql

def cat(self,ids,cat):

sql="INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (%s,%s,'0')"%(ids,cat)

return sql

def close_mysql(self,cursor,conn):

conn.commit()

cursor.close()

conn.close()

使用方法:

a=wordpress_post(str(t),wz_content) #这里第一个参数是标题 第二个是文章内容

conn=a.mysql_con()

cursor = conn.cursor()

c=a.up()

effect_row = cursor.execute(c)

new_id = cursor.lastrowid #这里是记录文章id以便设置文章的分类

d=a.cat(new_id,'1')

effect_row = cursor.execute(d)

a.close_mysql(cursor,conn)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值