僅需200多行代碼,網站變成機器人兒。用wordpress搭建的網站可以用程式控制完成自動采集資訊,并自動上傳更新。如果外部條件不變化(比如資料來源),理論上wordpress制作的網站可以在無人值守的狀态下一直自轉下去。程式用python寫成,需要安裝第三方包wordpress_xmlrpc。
以下是我們為某網站定制的自更新程式代碼,僅是資料上傳部分。供參考.
#coding:utf-8 本程式實作在www.okinawa-newlife.com網站自動發帖,别的wordpress網站可套用。
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import sys
import os
def picksn(): # 查找本地編号檔案,
# 本函數取得所有待處理房産編号,和已處理房産編号 作為list傳回給調用函數。沒有傳回空
global path
wfname = path + '房産編号.txt' #未處理的房産編号
yfname = path + '房産編号已傳.txt' #已處理的房産編号
if not os.path.isfile(wfname): # 判斷本地是否存在檔案是否存在,不存在就退出
print("房産編号檔案不存在。")
wlist = [""]
else:
with open(wfname, 'r',encoding='utf-8') as wfile: # 從檔案讀,如文中有漢字,encoding 參數必須有,否則報錯 UnicodeDecodeError。。。
wlist = wfile.readlines() # 整個檔案讀出放到清單中
if not os.path.isfile(yfname): # 判斷本地是否存在已處理編号的檔案
ylist = [""]
else:
with open(yfname, 'r', encoding='utf-8') as yfile: # 從檔案讀,如文中有漢字,encoding 參數必須有,否則報錯 UnicodeDecodeError。。。
ylist = yfile.readlines() # 整個檔案讀出放到清單中
sns = [wlist[0],ylist[0]] #把待處理編号和已處理編号都傳回給調用函數
return sns
def sendpost(sn):
para = getinfo(sn)
wp = Client('http://www.okinawa-newlife.com/xmlrpc.php', '使用者名', '密碼')
post = WordPressPost()
post.title = para[0]
post.content = para[1]+para[2]+para[3]+para[4]+para[5]+para[6]
post.post_status = 'publish' #文章狀态,不寫預設是草稿,private表示私密的,draft表示草稿,publish表示釋出
post.terms_names = {
'post_tag': ['沖繩房産', '日本房産','沖繩投資',para[1]], #文章所屬标簽,沒有則自動建立
'category': ['沖繩房産',para[1] ] #文章所屬分類,沒有則自動建立
}
# 以下代碼添加特色圖像
imgfname = path+sn+'.jpg' # 上傳的圖檔檔案路徑
if os.path.isfile(imgfname): # 判斷檔案名是否存在,有才上傳圖檔
# prepare metadata
data = {
'name': 'picture.jpg',
'type': 'image/jpeg', # mimetype
}
# read the binary file and let the XMLRPC library encode it into base64
with open(imgfname, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
attachment_id = response['id']
post.thumbnail = attachment_id # 縮略圖的id
post.id = wp.call(posts.NewPost(post))
print(sn+"号物業已上傳")
def getinfo(sn):
# 按編号從之前儲存的本地檔案讀取資訊
#para= [] # 儲存參數的list
filename = path+sn+'-有用資訊.txt'
with open(filename, 'r', encoding='utf-8') as f: # 從檔案讀,如文中有漢字,encoding 參數必須有,否則報錯 UnicodeDecodeError。。。
para = f.readlines() # 整個檔案讀出放到清單中
return para
#sns = filestr.split(",") # str轉為list, 逗号分隔
#程式起點在此
path = r'C:\20200714\房産素材\\' # 指定一個儲存檔案的檔案夾
sns = picksn() # 擷取編号清單,sns是嵌套list,他的值一個是待處理編号的list, 一個是已處理編号的list
wsns = sns[0].split(',') #待處理編号list
ysns = sns[1].split(',') #已處理編号list
#wsns = ['42779','42708']
#ysns = [""]
nsns = [""] #本次程式執行處理的編号list
yfname = path + '房産編号已傳.txt' #儲存已上傳編号的檔案名
for i in wsns: #把待處理編号中的值逐個處理
if i not in ysns: #判斷下是否在已處理編号中,沒有才繼續下邊的post程式
# ctn = input("将要上傳"+i+",繼續按回車,按n退出程式") #人工幹預是否繼續,按回車值為空,繼續
# if ctn == '':
filename = path+i+'-有用資訊.txt'
if os.path.isfile(filename): # 判斷檔案名是否存在,如不存在就處理下一個編号
sendpost(i)
# 把更新過的編号做個記錄,儲存本地檔案中,避免重複更新
with open(yfname, 'a', encoding='utf-8') as f:
f.write(","+i)
# else:
# sys.exit() #退出程式
print("上傳完成!")