Python wordpresslib将图像附加到帖子

Python wordpresslib将图像附加到帖子,python,wordpress,Python,Wordpress,查看由提供的示例,它非常直接地介绍了如何将图像上载到媒体库。然而,图像的附件似乎从未完成。有人成功地附加了这些图像吗 #!/usr/bin/env python """ Small example script that publish post with JPEG image """ # import library import wordpresslib print 'Example of posting.' print url = raw_input('Wordpress

查看由提供的示例,它非常直接地介绍了如何将图像上载到媒体库。然而,图像的附件似乎从未完成。有人成功地附加了这些图像吗

#!/usr/bin/env python

"""
    Small example script that publish post with JPEG image
"""

# import library
import wordpresslib

print 'Example of posting.'
print

url = raw_input('Wordpress URL (xmlrpc.php will be added):')
user = raw_input('Username:')
password = raw_input('Password:')

# prepare client object
wp = wordpresslib.WordPressClient(url+"xmlrpc.php", user, password)

# select blog id
wp.selectBlog(0)

# upload image for post
# imageSrc = wp.newMediaObject('python.jpg')

# FIXME if imageSrc:

# create post object
post = wordpresslib.WordPressPost()
post.title = 'Test post'
post.description = '''
Python is the best programming language in the earth !
No image BROKEN FIXME <img src="" />
'''
#post.categories = (wp.getCategoryIdFromName('Python'),)

# Add tags
post.tags = ["python", "snake"]

# do not publish post
idNewPost = wp.newPost(post, False)

print
print 'Posting successfull! (Post has not been published though)'

Wordpress将图像保存为
website.com/wp content/uploads/YEAR/MONTH/FILENAME

将上述格式的简单图像标签添加到
post中。description
在post上显示图像

其中,
YEAR
为当前年份,格式为4位数字(例如2015年)

MONTH
是前导为零的当前月份(例如01,02,…12)

FILENAME
是通过
imageSrc=wp.newMediaObject('python.jpg')提交的文件名

示例文件名:website.com/wp-content/uploads/2015/06/image.jpg

以下是我如何发布我的图片:

import time
import wordpresslib
import Image
from datetime import datetime

time = datetime.now()

h = str(time.strftime('%H'))
m = str(time.strftime('%M'))
s = str(time.strftime('%S'))
mo = str(time.strftime('%m'))
yr = str(time.strftime('%Y'))

url = 'WORDPRESSURL.xmlrpc.php'
wp = wordpresslib.WordPressClient(url,'USERNAME','PASSWORD')
wp.selectBlog(0)
imageSrc = wp.newMediaObject('testimage'+h+m+s'.jpg') #Used this format so that if i post images with the same name its unlikely they will override eachother

img = 'http://WORDPRESSURL/wp-content/uploads/'+yr+'/'+mo+'/testimage'+h+m+s+'.jpg'

post=wordpresslib.WordPressPost()
post.title='title'
post.description='<img src="'+img+'"/>'
idPost=wp.newPost(post,true)
导入时间
导入wordpresslib
导入图像
从日期时间导入日期时间
time=datetime.now()
h=str(time.strftime('%h'))
m=str(time.strftime('%m'))
s=str(time.strftime('%s'))
mo=str(time.strftime('%m'))
yr=str(time.strftime(“%Y”))
url='WORDPRESSURL.xmlrpc.php'
wp=wordpresslib.WordPressClient(url、‘用户名’、‘密码’)
wp.selectBlog(0)
imageSrc=wp.newMediaObject('testimage'+h+m+s'.jpg')#使用此格式,因此如果我发布同名图像,它们不太可能相互覆盖
img=http://WORDPRESSURL/wp-content/uploads/“+yr+”/“+mo+”/测试图像“+h+m+s+”.jpg”
post=wordpresslib.WordPressPost()
post.title='title'
post.description=''
idPost=wp.newPost(post,true)
import time
import wordpresslib
import Image
from datetime import datetime

time = datetime.now()

h = str(time.strftime('%H'))
m = str(time.strftime('%M'))
s = str(time.strftime('%S'))
mo = str(time.strftime('%m'))
yr = str(time.strftime('%Y'))

url = 'WORDPRESSURL.xmlrpc.php'
wp = wordpresslib.WordPressClient(url,'USERNAME','PASSWORD')
wp.selectBlog(0)
imageSrc = wp.newMediaObject('testimage'+h+m+s'.jpg') #Used this format so that if i post images with the same name its unlikely they will override eachother

img = 'http://WORDPRESSURL/wp-content/uploads/'+yr+'/'+mo+'/testimage'+h+m+s+'.jpg'

post=wordpresslib.WordPressPost()
post.title='title'
post.description='<img src="'+img+'"/>'
idPost=wp.newPost(post,true)