Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342453
  • 博文数量: 148
  • 博客积分: 2745
  • 博客等级: 少校
  • 技术积分: 1704
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-30 14:59
文章分类

全部博文(148)

文章存档

2013年(97)

2012年(7)

2011年(3)

2010年(41)

我的朋友

分类: Python/Ruby

2013-03-10 22:48:17

今天给大家分享下python在本地远程s,如果你的网站数据库是支持远程连接的话,那可以用下面的方法。
我 当初写这个代码是为了解决wordpress底层站群的文章同步问题,可以让本地的mysql数据通过python脚本远程插入到网站数据库里,从而可以 完成定时的更新。当然这个脚本如果部署到服务器上会更好,可以通过windows的计划任务和linux的cron服务来定期的启动这个脚本,从而达到每 天更新文章的目的。

写这个脚本主要是要熟悉wordpress的表结构,不然你没法插入数据到wordpress数据表。
代码如下:

wordpress 数据python同步方法

Python语言:
#encoding=utf-8
#description:同步wordpress文章数据
 
import MySQLdb
import datetime
import time
from  tools import *

def wp_checktitle(dbconn,title):
    '''wordpress检测是否有重复标题'''

    cursor=dbconn.cursor()

    sql = "select post_title from wp_posts where post_title='%s'" % (title)
    cursor.execute(sql)

    if cursor.rowcount == 0:
        checkflag = 1
    else:
        checkflag = 0

    return checkflag



def  sync_wordpress(dbconn,title,content):
    '''同步wordpress程序'''
   
    checkflag = wp_checktitle(dbconn,title)
    cursor=dbconn.cursor()
   
    curtime = str(datetime.datetime.now())[:19]
   
    post_author = 1
    post_date = curtime
    post_date_gmt = curtime
    post_content = content
    post_title = title
    post_name = post_title
    post_modified = curtime
    post_modified_gmt = curtime
    post_content_filtered = ''
    currenttime = int(time.time())
   

    if checkflag:
       try:
           postsql = ''
           postsql = '''INSERT INTO `wp_posts` (
                  `post_author` ,
                  `post_date` ,
                  `post_date_gmt` ,
                  `post_content` ,
                  `post_title` ,
                  `post_name` ,
                  `post_modified`,
                  `post_modified_gmt`,
                  `post_content_filtered`
                  )
                  VALUES (
                   '%(post_author)s','%(post_date)s','%(post_date_gmt)s','%(post_content)s','%(post_title)s','%(post_name)s','%(post_modified)s','%(post_modified_gmt)s','%(post_content_filtered)s')''' % {'post_author':post_author,'post_date':post_date,'post_date_gmt':post_date_gmt,'post_content':post_content,'post_title':post_title,'post_name':post_name,'post_modified':post_modified,'post_modified_gmt':post_modified_gmt,'post_content_filtered':post_content_filtered}
          

           cursor.execute(postsql)
           dbconn.commit()
          
           rowid = cursor.lastrowid
              
           metasql = ''
           metasql = "insert into `wp_postmeta`(`post_id`)VALUES(%s)" % (rowid)
           cursor.execute(metasql)
           dbconn.commit()

           insertsql = '''INSERT INTO `wp_term_relationships` (
           `object_id` ,
           `term_taxonomy_id`
           )
           VALUES (
           %(object_id)s, %(term_taxonomy_id)s) ''' % {'object_id':rowid,'term_taxonomy_id':1}
          
           cursor.execute(insertsql)
           dbconn.commit()

           return 1

       except Exception, e:
            print '数据库错误:', e
            return 0

       finally:
            cursor.close()
            dbconn.close()
    else:
        print 'wordpress title exist'
        return 1

title = 'titl-wptitle'
zcontent = 'content—————–'

curhost = ''##远程数据库服务器地址
webuser = ''#数据库用户名
webpwd =''#数据库密码
webdb = ''#数据库名称

dbconn = MySQLdb.connect(host=curhost, user=webuser, passwd=webpwd, db=webdb, port=3306, charset='utf8')
    
flag = sync_wordpress(dbconn,title,zcontent)

if flag:
    print 'wordpress sync success'
else:
    print 'wordpress sync error'
 
最后效果是:
 

如果你的网站数据库不支持远程链接的话,那只有通过python操作浏览器然后get数据过去了,这个我以后有时间会写写。大家有兴趣可以试试。

阅读(608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~