您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程

wordpress数据远程同步的实现方法

乐枫2025-03-27WordPress教程已有人查阅

导读今天给大家分享下python在本地远程同步文章数据到wordpress,如果你的网站数据库是支持远程连接的话,那可以用下面的方法。
我当初写这个代码是为了解决wordpress底层站群的文

今天给大家分享下python在本地远程同步文章数据到wordpress,如果你的网站数据库是支持远程连接的话,那可以用下面的方法。
我当初写这个代码是为了解决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'
相信用wordpress能够让你远程同步程序。

本文标签:

很赞哦! ()

相关教程

相关源码

  • 粉色家政月嫂保姆公司pbootcms网站模板(PC+WAP)为家政服务、月嫂保姆企业打造的营销型解决方案,基于PbootCMS内核开发,采用温馨粉色主题传递行业温度。PHP7.0+高性能架构支持SQLite/MySQL双数据库查看源码
  • pbootcms源码宠物类网站源码下载(自适应多端)为宠物装备商店、宠物食品及用品企业打造的营销型模板,基于PbootCMS内核深度开发。采用响应式设计实现PC与移动端适配,PHP7.0+高性能架构支持MySQL/SQLite双数据库查看源码
  • (自适应)个人图集图片相册画册pbootcms网站模板源码本模板基于PbootCMS系统开发,为图片展示类网站设计,特别适合个人作品集、摄影画册、艺术图集等内容展示。采用响应式布局技术,确保各类图片在不同设备上查看源码
  • pbootcms模板(PC+WAP)火锅加盟餐饮美食类带留言源码基于PbootCMS内核深度开发,为火锅、餐饮品牌打造的营销型解决方案。采用红色主题传递行业活力,实现PC与WAP端适配。查看源码
  • (PC+WAP)门窗门业家居定制铝合金产品pbootcms模板下载基于PbootCMS内核开发的门窗门业企业专用模板,采用响应式设计结构,数据一次录入即可同步适配电脑与手机端浏览。通过简洁大气的视觉呈现,帮助门窗企业快速建立专业线上展示平台,有效传递产品价值与服务优势。查看源码
  • (自适应)行业协会工会机构单位pbootcms网站源码本模板基于PbootCMS内核开发,为行业协会、工会组织及机构单位量身打造。采用响应式布局设计,可自动适配手机、平板等移动设备,数据实时同步更新。模板包含行业资讯查看源码
分享笔记 (共有 篇笔记)
验证码: