Contents
事前準備
・Pythonをインストール
Python公式サイトより入手してインストールします。
・tweepyをインストール
コマンドプロンプトで「pip install tweepy」と入力してインストールします。
・Twitter APIKeyの入手
Twitter API KeyはTwitter developerのページから登録申請が必要です。
Twitter APIKeyを入手するには厳密な審査があります。
Twitterよりツイートを取得するコード
実行環境:Python 3.7.4
テキストエディタでコードを保存して実行することでユーザ名とツイートと日時を取得できます。
# coding: shift_jis
import tweepy
consumer_key = 'Twitterで登録して入手したキー'
consumer_secret = 'Twitterで登録して入手したキー'
access_token_key = 'Twitterで登録して入手したキー'
access_token_secret = 'Twitterで登録して入手したキー'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
api = tweepy.API(auth)
wordstring = '言葉'
maxcount = 20
getresults = api.search(q=wordstring, count=maxcount)
for resultline in getresults:
print("---------------------------------")
username = resultline.user.name
print("ユーザー名:"+username)
tweetcontent = resultline.text
print("コメント:"+tweetcontent)
print(resultline.created_at)
・”Twitterで登録して入手したキー”は入手したAPIKeyを入力します。
・wordstringに任意の取得するキーワードを入力することで取得できます。
・maxcountにて20にしているため20件まで取得できます。
コメント