您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
wordpress数据远程同步的实现方法
乐枫2025-03-27WordPress教程已有人查阅
导读今天给大家分享下python在本地远程同步文章数据到wordpress,如果你的网站数据库是支持远程连接的话,那可以用下面的方法。
我当初写这个代码是为了解决wordpress底层站群的文
今天给大家分享下python在本地远程同步文章数据到wordpress,如果你的网站数据库是支持远程连接的话,那可以用下面的方法。
我当初写这个代码是为了解决wordpress底层站群的文章同步问题,可以让本地的mysql数据通过python脚本远程插入到网站数据库里,从而可以完成定时的更新。当然这个脚本如果部署到服务器上会更好,可以通过windows的计划任务和linux的cron服务来定期的启动这个脚本,从而达到每天更新文章的目的。
写这个脚本主要是要熟悉wordpress的表结构,不然你没法插入数据到wordpress数据表。
代码如下:
wordpress 数据python同步方法Python语言: 高亮代码由发芽网提供
我当初写这个代码是为了解决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能够让你远程同步程序。
本文标签:
很赞哦! ()
相关教程
图文教程
wordpress文章存放位置,wordpress的优缺点有哪些
wordpress的文章是存在数据库中的,如博客发过的文章存在表 wp_post中。标签和分类等在其他的表中。如果你想了解更多关于wordpress的知识
wordpress设置二级分类目录的方法
wordpress设置二级分类目录的步骤,进入该网站的界面,点击w图标,点击文章,点击分类目录。
wordpress多站点使用同一个数据库的方法
尚未安装WordPress在初始配置WordPress之前,将wp-config.php中的$table_prefix值(默认为wp_)修改为所需要的表前缀(本文中假设为blog_)
wordpress怎么添加自定义按钮并导出csv
wordpress 后台添加自定义按钮导出csv在wp-admin/edit.php中找到如下代码:
相关源码
-
(自适应)电梯扶梯升降梯行业pbootcms企业网站模板(自适应手机版)响应式电梯扶梯类pbootcms模板 电梯生产企业绿色企业网站源码下载PbootCMS内核开发的网站模板,该模板适用于电梯、扶梯类等企业,查看源码 -
pbootcms模板(PC+WAP)火锅加盟餐饮美食类带留言源码基于PbootCMS内核深度开发,为火锅、餐饮品牌打造的营销型解决方案。采用红色主题传递行业活力,实现PC与WAP端适配。查看源码 -
帝国CMS7.5漫画网站模板带手机端源码免费下载本模板为漫画内容平台设计开发,采用帝国CMS7.5内核构建,深度优化漫画作品展示结构与章节管理模式。前端采用响应式布局设计,适配各类漫画阅读场景,提供作品分类、连载追踪、热度排行等垂直领域功能模块。查看源码 -
(自适应响应式)高端简繁双语HTML5金融资本咨询单页pbootcms模板采用响应式设计确保在各类手机端设备很好的呈现。该模板专注于金融咨询、资本管理等领域企业形象展示,通过结构化布局突出行业专业度与可信度,后台数据同步管理简化内容维护流程。查看源码 -
pbootcms模板(自适应手机版)红色响应式单位机构类网站自适应响应式单位机构网站模板 | PbootCMS内核开发为机构组织设计的响应式网站模板,采用PbootCMS内核开发,支持一键替换行业内容,满足多元化场景需求。查看源码 -
(自适应)五金配件机械蓝色营销型pbootcms模板免费下载基于PbootCMS的工业配件展示系统,精简DIV+CSS架构确保响应速度,自适应设计呈现参数表格,支持后台实时更新产品规格、在线留言等内容。查看源码
| 分享笔记 (共有 篇笔记) |

