LoginSignup
3

More than 1 year has passed since last update.

PythonでTweepyを使ってTwitterをごにょごにょする

Last updated at Posted at 2021-05-12

TwitterDevelopers

事前設定

初期設定はReadOnlyになっているでツイートするとエラーが発生する。
設定の変更が必要である。
設定を変更したら、各トークンの再発行が必要とのこと!
https://qiita.com/butsuli_shine/items/78fd5ee6fdb4a0581652

基本系

import tweepy

# 認証に必要なキーとトークン
API_KEY = 'xxxxxxxxxxx'
API_SECRET = 'xxxxxxxxxxxxxx'
ACCESS_TOKEN = 'xxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxx'

# APIの認証
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

キーワードに関連するツイートを取得


# キーワードからツイートを取得
api = tweepy.API(auth)
tweets = api.search(q=['くるま'], count=10)

for tweet in tweets:
    print('-----------------')
    print(tweet.text)

普通にツイート


api = tweepy.API(auth)

#tweet
api.update_status("hello tweepy")

画像付きでツイート

api = tweepy.API(auth)

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

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
3