您现在的位置是:首页 > 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内核开发的响应式网站模板,为设计工作室、创意机构打造的作品展示解决方案。通过模块化布局与极简交互设计,呈现设计作品的视觉细节,支持作品分类、案例解析等多维度展示方式。查看源码
  • pbootcms网站模板响应式全屏旅游景区网站源码本模板为风景民宿、旅游景区等企业设计,基于PbootCMS内核开发,具备响应式布局与专业SEO优化功能,助力企业低成本高效获客。以下是核心特点:查看源码
  • (PC+WAP)货运物流快递仓储货架pbootcms网站模板下载本模板基于PbootCMS开发,为货运物流、快递配送、仓储货架等行业设计,支持PC+WAP自适应,确保在电脑、手机、平板等设备上均能流畅访问。适用于物流公司、仓储企业查看源码
  • (自适应)幽默笑话脑筋急转弯搞笑趣图博客pbootcms模板下载本模板基于PbootCMS系统开发,专为幽默笑话、搞笑趣图类网站设计。采用轻松活泼的布局风格,突出娱乐内容分享特色,适合各类笑话、段子、搞笑图片等内容展示。查看源码
  • (自适应响应式)英文外贸医疗科研耗材设备pbootcms网站模板为医疗设备和外贸企业设计的响应式网站模板,基于PbootCMS系统开发。突出医疗产品认证展示和国际化特性,通过专业化的产品参数展示模块和文档管理系统,满足医疗行业严格的信息披露要求。查看源码
  • (自适应响应式)英文外贸电子产品手机配件网站pbootcms模板专业级外贸网站模板基于PbootCMS内核开发,为电子产品跨境贸易企业设计。采用响应式布局技术,确保电脑、平板、手机等设备访问时自动适配显示效果。数据实时同步管理,一次编辑全平台生效。查看源码
分享笔记 (共有 篇笔记)
验证码: