Today, we will share with you how to remotely synchronize Python locally.ArticleData to Wordpress. If your website database supports remote connection, you can use the following method.
I wrote thisCodeTo solve the article synchronization problem of the WordPress underlying website group, you can remotely insert local Mysql Data to the website database through the Python script, so that regular updates can be completed. Of course, it would be better to deploy this script on the server. You can use Windows scheduled tasks and Linux Cron services to regularly start this script, so as to update the article every day.
To write this script, you must be familiar with the WordPress table structure. Otherwise, you cannot insert data into the WordPress data table.
The Code is as follows:
Python synchronization method for WordPress data
Python language : The highlighted code is provided by the germination network. # Encoding = UTF-8
# Description: Synchronize WordPress Article data
Import mysqldb
Import datetime
Import time
From tools import *
DefWp_checktitle (dbconn, title ):
'''Wordpress check for repeated headers '''
Cursor = dbconn. cursor ()
SQL ="Select post_title from wp_posts where post_title ='% S'"% (Title)
Cursor.exe cute (SQL)
IfCursor. rowcount =0:
Checkflag =1
Else:
Checkflag =0
ReturnCheckflag
Def Sync_wordpress (dbconn, title, content ):
''' Synchronize WordPressProgram'''
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.exe cute (postsql)
dbconn. commit ()
rowid = cursor. lastrowid
metasql = ''
metasql = " insert into 'wp _ postmeta' ('Post _ id') values ( % S ) " % (rowid)
cursor.exe cute (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.exe cute (insertsql)
Dbconn. Commit ()
Return 1
ExceptException, E:
Print 'Database error :', E
Return 0
Finally:
Cursor. Close ()
Dbconn. Close ()
Else:
Print 'Wordpress title exist'
Return 1
Title ='Titl-wptitle'
Zcontent ='Content ------'
Curhost =''# Remote database server address
Webuser =''# Database username
Webpwd =''# Database Password
Webdb =''# Database Name
Dbconn = mysqldb. Connect (host = curhost, user = webuser, passwd = webpwd, DB = webdb, Port =3306, Charset ='Utf8')
Flag = sync_wordpress (dbconn, title, zcontent)
IfFlag:
Print 'Wordpress sync SUCCESS'
Else:
Print 'Wordpress sync error'I believe that using WordPress can allow you to remotely synchronize programs.