使用tweepy库将一些帖子中的多个图像上传到twitter

时间:2015-08-09 21:09:36

标签: image twitter tweepy

用图像更新没有问题。这是代码,它就是wotks:

import tweepy
from subprocess import call
from datetime import datetime
import time

...key and tokens...

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
photo_path = '/directory/image.jpg'
status = 'Subject o text'
api.update_with_media(photo_path, status=status)

但如果我想要发布包含多个图片的推文怎么办?用这个库可以做到这一点吗?

1 个答案:

答案 0 :(得分:1)

由于Twitter不推荐使用update_with_media端点,因此您应该使用media_upload,如下所示:

api = tweepy.API(auth)
images = ('image1.png', 'image2.png')
media_ids = [api.media_upload(i).media_id_string  for i in images] 
status = 'Subject o text'
api.update_with_media(media_ids=media_ids, status=status)