WP Post

WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统。

全球大约有30%的网站(7亿5000个)都是使用WordPress架设网站的。最近在学习Python爬虫的时候,发现可以利用Python来发布WordPress文章。这样一来就相当有趣了,比如使用爬虫然后将结果通过WordPress在线发布。

使用的软件库为:wordpress_xmlrpc。帮助文件地址

对于Python3版本的安装命令为:

1
pip3 install python-wordpress-xmlrpc

以最简单的发布为例,代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
 
wp = Client('https://www.xxxxxx.xxx/xmlrpc.php', '用户名', '密码')
 
post = WordPressPost()
post.title = 'Hello'
post.content = 'Good to See You!'
post.post_status = 'publish'
post.terms_names = {
    'post_tag': ['test', 'world'],
  'category': ['Introductions', 'Tests']
}
wp.call(NewPost(post))

在此基础上,还可以结合Python爬虫,将爬虫结果和自动发布结合。同时,对于监测的数据,可以定期运行程序。比如在服务器上采用cron软件,按每小时/每天/每月/...定期发布。

updatedupdated2020-08-072020-08-07