均益采集文章,一般是用python,而网站程序是wordpress的,本来想直接将文章插入数据库的,但是wordpress的数据表有点麻烦。
所以均益使用python的 wordpress_xmlrpc模块,这个模块的使用方法请看 https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
这个模块是国外开发的,说明文档也是英文的,看起来吃力的朋友。可以看一下均益总结这个模块常用的方法。
安装:
easy_install python-wordpress-xmlrpc 或者 pip install python-wordpress-xmlrpc
带有自定义栏目字段的发布文章代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#coding:utf-8from wordpress_xmlrpc import Client, WordPressPostfrom wordpress_xmlrpc.methods.posts import GetPosts, NewPostfrom wordpress_xmlrpc.methods.users import GetUserInfofrom wordpress_xmlrpc.methods import postsfrom wordpress_xmlrpc.methods import taxonomiesfrom wordpress_xmlrpc import WordPressTermfrom wordpress_xmlrpc.compat import xmlrpc_clientfrom wordpress_xmlrpc.methods import media, postsimport sysreload(sys)sys.setdefaultencoding('utf-8')post = WordPressPost()post.title = '文章标题'post.content = '文章内容'post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布post.terms_names = { 'post_tag': ['test', 'firstpost'], #文章所属标签,没有则自动创建 'category': ['Introductions', 'Tests'] #文章所属分类,没有则自动创建 }post.custom_fields = [] #自定义字段列表post.custom_fields.append({ #添加一个自定义字段 'key': 'price', 'value': 3})post.custom_fields.append({ #添加第二个自定义字段 'key': 'ok', 'value': '天涯海角'})post.id = wp.call(posts.NewPost(post)) |
带有特色图像缩略图的发布文章
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#coding:utf-8from wordpress_xmlrpc import Client, WordPressPostfrom wordpress_xmlrpc.methods.posts import GetPosts, NewPostfrom wordpress_xmlrpc.methods.users import GetUserInfofrom wordpress_xmlrpc.methods import postsfrom wordpress_xmlrpc.methods import taxonomiesfrom wordpress_xmlrpc import WordPressTermfrom wordpress_xmlrpc.compat import xmlrpc_clientfrom wordpress_xmlrpc.methods import media, postsimport sysreload(sys)sys.setdefaultencoding('utf-8')filename = './my.jpg' #上传的图片文件路径# prepare metadatadata = { 'name': 'picture.jpg', 'type': 'image/jpeg', # mimetype}# read the binary file and let the XMLRPC library encode it into base64with open(filename, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read())response = wp.call(media.UploadFile(data))# response == {# 'id': 6,# 'file': 'picture.jpg'# 'type': 'image/jpeg',# }attachment_id = response['id']post = WordPressPost()post.title = '文章标题'post.content = '文章正文'post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布post.terms_names = { 'post_tag': ['test', 'firstpost'], #文章所属标签,没有则自动创建 'category': ['Introductions', 'Tests'] #文章所属分类,没有则自动创建 }post.thumbnail = attachment_id #缩略图的idpost.id = wp.call(posts.NewPost(post)) |
除了可以发布文章,这个模块也可以单独创建新的分类和标签
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#coding:utf-8from wordpress_xmlrpc import Client, WordPressPostfrom wordpress_xmlrpc import WordPressTermfrom wordpress_xmlrpc.methods import taxonomiesimport sysreload(sys)sys.setdefaultencoding('utf-8')#新建标签tag = WordPressTerm()tag.taxonomy = 'post_tag'tag.name = 'My New Tag12'#标签名称tag.slug = 'bieming12'#标签别名,可以忽略tag.id = wp.call(taxonomies.NewTerm(tag))#返回的id#新建分类cat = WordPressTerm()cat.taxonomy = 'category'cat.name = 'cat1'#分类名称cat.slug = 'bieming2'#分类别名,可以忽略cat.id = wp.call(taxonomies.NewTerm(cat))#新建分类返回的id#新建子分类parent_cat = client.call(taxonomies.GetTerm('category', 20))#20是父分类的idchild_cat = WordPressTerm()child_cat.taxonomy = 'category'child_cat.parent = parent_cat.idchild_cat.name = 'My Child Category'#分类名称child_cat.slug = 'beidongdui'#分类别名,可以忽略child_cat.id = wp.call(taxonomies.NewTerm(child_cat))#新建分类返回的id |
转载请注明:均益个人博客 » python使用xmlrpc自动发布文章到wordpress


0回复python使用xmlrpc自动发布文章到wordpress"