要實(shí)現(xiàn) Python 自動(dòng)將內(nèi)容發(fā)布到 WordPress,可以使用 Python 的 wordpress_xmlrpc?庫(kù),該庫(kù)提供了使用 WordPress XML-RPC API 進(jìn)行內(nèi)容發(fā)布和管理的功能。 需要安裝一下第三方庫(kù):wordpress_xmlrpc!
pip install python_wordpress_xmlrpc
下面是一個(gè)簡(jiǎn)單的示例代碼,可以實(shí)現(xiàn)發(fā)布一篇新文章到 WordPress:
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods import posts
# WordPress 網(wǎng)站的 XML-RPC API 地址和賬號(hào)信息
url = “網(wǎng)站域名/xmlrpc.php”
username = “your-username”
password = “your-password”
# 連接 WordPress 網(wǎng)站并進(jìn)行身份驗(yàn)證
client = Client(url, username, password)
# 創(chuàng)建一個(gè)新的 WordPress 文章
post = WordPressPost()
post.title = “這是一篇新文章”
post.content = “這是文章的內(nèi)容?!?/p>
# 文章?tīng)顟B(tài),不寫默認(rèn)是草稿,private表示私密的,draft表示草稿,publish表示發(fā)布
post.post_status = “draft”
post.terms_names = {
????‘post_tag’: [‘test’, ‘firstpost’], #文章所屬標(biāo)簽,沒(méi)有則自動(dòng)創(chuàng)建
????‘category’: [‘Introductions’, ‘Tests’] #文章所屬分類,沒(méi)有則自動(dòng)創(chuàng)建
?}
# 發(fā)布文章到 WordPress
client.call(posts.NewPost(post))
在上面的代碼中,我們首先指定了 WordPress 網(wǎng)站的 XML-RPC API 地址和賬號(hào)信息,然后創(chuàng)建了一個(gè)新的 WordPress 文章對(duì)象,并設(shè)置了文章的標(biāo)題、內(nèi)容和狀態(tài)。最后,我們使用 client.call 方法調(diào)用 posts.NewPost 方法,將文章發(fā)布到 WordPress 網(wǎng)站。
需要注意的是,在使用 WordPress XML-RPC API 時(shí),需要遵守相關(guān)的使用規(guī)定和安全措施,例如使用 HTTPS 進(jìn)行數(shù)據(jù)傳輸,避免明文傳輸密碼等。此外,在使用 API 時(shí),也需要控制訪問(wèn)頻率和并發(fā)請(qǐng)求數(shù)量,避免給 WordPress 網(wǎng)站帶來(lái)過(guò)大的負(fù)載壓力。
??如果出現(xiàn)下面的錯(cuò)誤:
Traceback (most recent call last):
??File “/root/newcode/python/wordpress_publish.py”, line 8, in <module>
????client = Client(url, username, password)
??File “/root/anaconda3/lib/python3.9/site-packages/wordpress_xmlrpc/base.py”, line 24, in __init__
????self.supported_methods = self.server.mt.supportedMethods()
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 1122, in __call__
????return self.__send(self.__name, args)
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 1464, in __request
????response = self.__transport.request(
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 1166, in request
????return self.single_request(host, handler, request_body, verbose)
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 1182, in single_request
????return self.parse_response(resp)
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 1348, in parse_response
????p.feed(data)
??File “/root/anaconda3/lib/python3.9/xmlrpc/client.py”, line 451, in feed
????self._parser.Parse(data, False)
xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 2, column 0
修改上面提示的/root/anaconda3/lib/python3.9/xmlrpc/client.py文件,將其中的:
def feed(self, data):
self._parser.Parse(data, 0)
改成如下的代碼:
def feed(self, data):
self._parser.Parse(data.strip(), 0)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-669511.html
然后再次測(cè)試,終于成功了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-669511.html
到了這里,關(guān)于python自動(dòng)把內(nèi)容發(fā)表到wordpress完整示例及錯(cuò)誤解答的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!