2017-04-20 24 views
1

我在Wordpress中有大約30,000個非常短的帖子。我使用SQLlite數據庫和python腳本構建它們,並通過python中的wordpress_xmlrpc庫上傳它們。但是我的網站在100-200個帖子後出現故障,因爲服務器認爲這是一個DoS攻擊。將30,000個帖子上傳到Wordpress而不會導致XML-RPC崩潰

我的服務器是我有SSH訪問權限的Linux機器。我正在尋找一種方式,通過直接與數據庫進行交互,或使用直接在服務器上發生的本地進程,更直接地將文章輕鬆上傳到Wordpress中。任何人可以提供任何想法?

+1

您是否嘗試過直接使用要添加的帖子恢復SQL數據庫? –

回答

4

我試圖通過直接與數據庫交互通過Python腳本來做同樣的事情,它對我來說就像一個魅力。我正在使用MySQL數據庫。

爲此,您需要ssh到Wordpress站點和數據庫託管的服務器。還有運行以下腳本:

油水運行該腳本:

一個。所有帖子應該位於一個 目錄內的不同文件中。

b。每個文件應在第一行包含帖子標題,並在其餘行中張貼內容 。

#!/usr/bin/env python 
import MySQLdb 
import fnmatch 
import os 

#List to contain all the post files 
my_match = [] 

#Gather post files in above list 
for file in os.listdir("<path of the directory where post files remains>"): 
    if fnmatch.fnmatch(file, '.*'): 
     print(file) 
     continue 
    my_match.append(file) 

print my_match 

#Make database connection 
conn = MySQLdb.connect(host= "localhost", user="<username>", passwd="<password>", db="<database name>") 

x = conn.cursor() 
print x 

for fl in my_match: 
    new_file = "<path to the directory where post files remains>/" + fl 
with open(new_file) as f: 
    heading = f.readline().strip() 
    content = f.read() 


print heading 
url = heading.replace(" ", "-") 

print url 

#try db query, change according to your database and tables 
try: 
    x.execute("""INSERT INTO wp_posts (post_author, post_date, post_content, post_title, post_name) VALUES (3, "2017-03-28 20:24:12", %s, %s, %s)""",(content, heading, url)) 
    conn.commit() 
    print "Done! :)" 
except: 
    conn.rollback() 
    print "Oops, not done :(" 

conn.close() 
+0

我會立即嘗試,如果它能正常工作,請回復您。謝謝!! – AstroBen

+0

Rohini,我很好奇...在sql數據庫中有很多其他字段的帖子...你使用WordPress的控制面板「重建數據庫」功能來更新所有這些? – AstroBen

+0

否AstroBen,'wp_posts'表的所有其他字段可以爲NULL或設置其默認值。這就是爲什麼我沒有爲他們提供任何價值,他們也不是我用的。 –

0

找到另一種方式,在這種情況下,任何人絆倒。將所有帖子轉儲到csv文件並使用「csv發佈」小部件。我正在使用Ultimate CSV Importer Free。很簡單。