国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
wordpress 數(shù)據(jù)遠(yuǎn)程同步方法

  今天給大家分享下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ì)寫寫。大家有興趣可以試試。

平均得分4.67分(共3次打分)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
20542通過(guò)wordpress
wordpress的tag函數(shù)使用教程
WordPress無(wú)插件調(diào)用最新、熱門、隨機(jī)文章實(shí)例代碼【wordpress隨機(jī)文章】
WordPress調(diào)用最新、熱門、隨機(jī)文章
WordPress數(shù)據(jù)庫(kù)及各表結(jié)構(gòu)分析
讓你的WordPress博客實(shí)現(xiàn)前臺(tái)快捷發(fā)布文章 | 螞蟻博客
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服