12
22

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.

Twitter APIを使ってツイートを取得(Python)

Last updated at Posted at 2018-03-14

#事前準備

#特定のツイートを取得
Twitter APIを利用する際に便利なPythonモジュール、tweepyを使います。インストールしていなければpip install tweepy してください。

get_tweets.py
import tweepy

consumer_key    = ""
consumer_secret = ""
access_key      = ""
access_secret   = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

tweets = api.search(q='ホーキング博士', count=10)
for tweet in tweets:
    print(tweet.text, "/n")

consumer_key、consumer_secret、access_key、access_secretにはアプリケーションで登録した4つの情報を入れてください。

api.search のq=は検索語、count=は取得するツイート数を指定します。なお、Twitter
APIには制限があるため気をつけましょう。

参考までに
https://qiita.com/mpyw/items/32d44a063389236c0a65
公式APIリファレンス:https://developer.twitter.com/en/docs/api-reference-index
今回使ったAPI:https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets

12
22
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
12
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?