用python发布文章到wordpress

 4年前     358  

文章目录

    第一步、安装Library

    pip install python-wordpress-xmlrpc

    第二步、开始发文

    from wordpress_xmlrpc import Client, WordPressPost
    from wordpress_xmlrpc.methods.users import GetUserInfo
    from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
     
    #网站的发文账户
    id="username"
    password="password"
     
    #網站網址,請把example.com替換成你的網址,並且先試著連上該網址,應該會出現「XML-RPC server accepts POST requests only.」才對。
    url="https://example.com/xmlrpc.php"
     
    #新文章要直接發布的話,就不用改,如果要變成草稿,就改成"draft"
    which="publish"
    #which="draft"
     
    #建立客戶端
    wp = Client(url, id,password)
     
    #建立新文章
    post = WordPressPost()
    post.post_status = which
    post.title = "新文章標題"
    post.content = "新文章內容"
    post.excerpt = "新文章內容摘要"
    post.terms_names = {
    "post_tag": ["標籤一","標籤二"]
    }
     
    #如果這一篇是過去的文章,可以透過這個方式指定該文章發表的日期。
    #post.date=datetime.strptime("2018/1/01 10:05:10","%Y/%m/%d %H:%M:%S")
    #發出去!
    wp.call(NewPost(post))
    

    打开网站就能看到已经发布的文章。

    【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
    版权声明:ADMIN 发表于 4年前,共 758 字。
    转载请注明:用python发布文章到wordpress | 奎利