1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tweepyで複数の画像を投稿する。

Last updated at Posted at 2019-01-22

使用するライブラリ

tweepy 3.6.0

メソッド

tweepyで複数の画像を投稿するためには

  • media_upload メソッド
  • update_status メソッド

を使用します。

スクリプトを書く

tweet_multiple_image.py
import tweepy

# トークンを入れる
CK= ''
CS= ''
AT= ''
AS= ''

auth= tweepy.OAuthHandler(CK,CS)
auth.set_access_token(AT,AS)

api= tweepy.API(auth)

# ツイートしたい文章
status= ''

# ツイートに付加したい画像をimagesリストに入れる
# media_idsは投稿した画像のidを入れるためのリスト
images= []
media_ids= []

# 画像は4枚までしか投稿できないので4回ループする
for i in range(4):
    img= api.media_upload(images[i])
    media_ids.append(img.media_id_string)

    #リスト内包記で書く場合
    #media_ids= [api.media_upload(images[i]).media_id_string for i in range(4)]

    media_ids= ','.join(media_ids)

# ツイートとして投稿しているのはここ
api.update_status(status=status,media=[media_ids])

画像付きのツイートは上のスクリプトで動きます。投稿した画像のサイズが極端に小さいと下の画像のようにURL表示されます。
IMG_E8285.JPG
一応、ツイートをクリックすれば、表示されます。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?