首页/Python/正文

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)

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • 😕