今天給大家分享下python在本地遠(yuǎn)程同步文章數(shù)據(jù)到wordpress,如果你的網(wǎng)站數(shù)據(jù)庫(kù)是支持遠(yuǎn)程連接的話,那可以用下面的方法。
我當(dāng)初寫這個(gè)代碼是為了解決wordpress底層站群的文章同步問(wèn)題,可以讓本地的mysql數(shù)據(jù)通過(guò)python腳本遠(yuǎn)程插入到網(wǎng)站數(shù)據(jù)庫(kù)里,從而可以完成定時(shí)的更新。當(dāng)然這個(gè)腳本如果部署到服務(wù)器上會(huì)更好,可以通過(guò)windows的計(jì)劃任務(wù)和linux的cron服務(wù)來(lái)定期的啟動(dòng)這個(gè)腳本,從而達(dá)到每天更新文章的目的。
寫這個(gè)腳本主要是要熟悉wordpress的表結(jié)構(gòu),不然你沒(méi)法插入數(shù)據(jù)到wordpress數(shù)據(jù)表。
代碼如下:
wordpress 數(shù)據(jù)python同步方法
#encoding=utf-8
#description:同步wordpress文章數(shù)據(jù)
import MySQLdb
import datetime
import time
from tools import *
def wp_checktitle(dbconn,title):
'''wordpress檢測(cè)是否有重復(fù)標(biāo)題'''
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 '數(shù)據(jù)庫(kù)錯(cuò)誤:', e
return 0
finally:
cursor.close()
dbconn.close()
else:
print 'wordpress title exist'
return 1
title = 'titl-wptitle'
zcontent = 'content—————–'
curhost = ''##遠(yuǎn)程數(shù)據(jù)庫(kù)服務(wù)器地址
webuser = ''#數(shù)據(jù)庫(kù)用戶名
webpwd =''#數(shù)據(jù)庫(kù)密碼
webdb = ''#數(shù)據(jù)庫(kù)名稱
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'
最后效果是:
如果你的網(wǎng)站數(shù)據(jù)庫(kù)不支持遠(yuǎn)程鏈接的話,那只有通過(guò)python操作瀏覽器然后get數(shù)據(jù)過(guò)去了,這個(gè)我以后有時(shí)間會(huì)寫寫。大家有興趣可以試試。