编程语言
首页 > 编程语言> > 使用Python将图片发布到Tumblr

使用Python将图片发布到Tumblr

作者:互联网

我正在尝试使用python将图片发布到tumblr,尤其是:http://code.google.com/p/python-tumblr/

#!/usr/bin/python

from tumblr import Api
import sys

BLOG='example.tumblr.com'
USER='example@example.com'
PASSWORD='example'
api = Api(BLOG,USER,PASSWORD)
post_data = "picture.png"   
title = "Title of my entry"
body = "this is a story with a picture"

api.write_regular(title, body + post_data)

当我运行此命令时,结果是博客到达,而不是:

Title of my entry

this is a story with a picture

[img]

我得到这个:

Title of my entry

this is a story with a
picturepicture.png

解决方法:

在您当前的代码中,您没有发布图像,而是发送了一个称为“ picture.png”的字符串.正如Daniel DiPaolo所说,您必须使用写照片.例如,write_photo的参数是指向图像的链接.

#!/usr/bin/python
from tumblr import Api
import sys

BLOG='example.tumblr.com'
USER='example@example.com'
PASSWORD='example'
api = Api(BLOG,USER,PASSWORD)
api.write_photo('http://example.org/somewhere/lolcat.jpg')

如果要发送HTML,则可以创建一个长为包含所选标签的正文.

title = "life is amazing" 
body = """
_here my html code_
"""

然后用API编写

api.write_regular(title,body)

而且你应该准备好了.

资料上传

更精确地说;)如果要发送数据,则必须打开对象.
假设您的图片是“ lolcat.jpg”

数据= open(‘lolcat.jpg’).read()

标签:python,tumblr
来源: https://codeday.me/bug/20191023/1913652.html