[Python]Tweepyでツイートを画像付きで投稿する

python

Tweepyを使ってテキストツイート投稿。さらに画像付きのツイートの方法も紹介。

 

テキストのみでツイート

import tweepy

consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'


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

api = tweepy.API(auth)

#tweet
api.update_status("hello tweepy")

 

画像付きツイート

import tweepy

consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'


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

api = tweepy.API(auth)

#tweet
api.update_with_media(status = "hello tweepy with media", filename = 'hoge.jpg')

filenameにはローカルに配置した画像パスを置くこと。

画像URLにも対応していると思ったが、対応してないっぽい。find notのエラーが返ってくる。

 

改行ツイート

テキストのみ、画像付きツイート以外にも改行したツイートもしたい。特にハッシュタグ利用時などは。

そうしたケースでは、単に\nを記載するだけで改行できる。



カテゴリー