我試圖通過直接與數據庫交互通過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()
您是否嘗試過直接使用要添加的帖子恢復SQL數據庫? –