目的
- ツイートからトレンドを把握する方法を身に着ける
手段
- Twitter API
- tweepy
実験で分かったこと
方法
- ツイートの取得
getTimeLine.py
import tweepy
def remove_url(text):
return text.split('https')[0] #URLで分割して前半のみを取り出す
def gettwitterdata():
# APIキー設定(別ファイルのconfig.pyで定義しています)
Consumer_key = '各自'
Consumer_secret = '各自'
Access_token = '各自'
Access_secret = '各自'
#認証
auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
auth.set_access_token(Access_token, Access_secret)
api = tweepy.API(auth, wait_on_rate_limit = True)
#検索キーワード設定
q = 'アスパラガス'
#screen_name ='elonmusk'
#つぶやきを格納するリスト
tweets_data =[]
#カーソルを使用してデータ取得
for i, tweet in enumerate(tweepy.Cursor(api.search_tweets, q=q, count=200,tweet_mode='extended').items()):
#for tweet in tweepy.Cursor(api.user_timeline,count=200,screen_name=screen_name,include_rts=False,exclude_replies = True).items():
#つぶやきテキスト(FULL)を取得
# print(tweet.text)
# text =remove_url(tweet.text)
print(tweet.full_text)
text =remove_url(tweet.full_text)
tweets_data.append(text + '\n')
#出力ファイル名
fname = r"'"+ './data.txt' + "'"
fname = fname.replace("'","")
#ファイル出力
with open(fname, "w",encoding="utf-8") as f:
f.writelines(tweets_data)
if __name__ == '__main__':
gettwitterdata()
- worlcloud化
getTimeLine.py
import MeCab
from wordcloud import WordCloud
FILE_NAME = "data.txt"
with open(FILE_NAME, "r", encoding="utf-8") as f:
CONTENT = f.read()
tagger = MeCab.Tagger("-Owakati")
parse = tagger.parse(CONTENT)
# fontが無いと日本語は文字化けする
fpath = "./ipaexg.ttf"
#wordcloud.generate(CONTENT)
wordcloud = WordCloud(background_color="white", font_path=fpath, width=900, height=500,
stopwords={'https','http','の','が','を','も','た','と','で','か','から','に','は','て','し','な','だ','する','てる','お','こと','これ'
'って','いう','この','なる','たら','たい','さん','ある','ない',
'これ','けど','ん','なっ','や','つ','て','な'}).generate(parse)
wordcloud.to_file("wordcloud.png")