pythonのtweepyでツイートを取得してwordcloudで表示したい

python

1import tweepy 2from datetime import datetime,timezone 3import pytz 4import pandas as pd 5import collections 6import MeCab 7import datetime 8import matplotlib.pyplot as plt 9from wordcloud import WordCloud 10import seaborn as sns 11sns.set(font='yuminl.ttf')12 13CONSUMER_KEY = '888'14CONSUMER_SECRET = '888'15ACCESS_TOKEN = '888'16ACCESS_SECRET = '888'17 18auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)19auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)20api = tweepy.API(auth)21 22search_results = api.search_tweets(q="ゲーム", result_type="recent",tweet_mode='extended',count=8)23 24tw_data = []25for tweet in search_results:26 #tweet_dataの配列に取得したい情報を入れていく27 tw_data.append([28 tweet.id,29 tweet.full_text,30 tweet.favorite_count, 31 tweet.retweet_count, 32 tweet.user.id, 33 tweet.user.screen_name,34 tweet.user.name,35 tweet.user.description,36 tweet.user.friends_count,37 tweet.user.followers_count,38 tweet.user.following,39 tweet.user.profile_image_url,40 tweet.user.profile_background_image_url,41 tweet.user.url 42 ])43 44#取り出したデータをpandasのDataFrameに変換45#CSVファイルに出力するときの列の名前を定義46labels=[47 'ツイートID',48 'ツイート本文',49 'いいね数',50 'リツイート数',51 'ID',52 'ユーザー名',53 'アカウント名',54 '自己紹介文',55 'フォロー数',56 'フォロワー数',57 '自分のフォロー状況',58 'アイコン画像URL',59 'ヘッダー画像URL',60 'WEBサイト'61 ]62 63#tw_dataのリストをpandasのDataFrameに変換64df = pd.DataFrame(tw_data,columns=labels)65num = 266num2 = 367counts = 168while counts <= 5:69 df[counts]=df.iat[num,1]70 df[counts+1]=df.iat[num2,1]71 tw_text=df[counts] + df[counts+1]72 counts += 273 num += 274 num2 += 275f=open('text.txt','w',encoding='UTF-8')76f.write(str(tw_text))77f.close()78 79f= open("text.txt", 'r', encoding='UTF-8') 80text=f.read()81f.close()82text=str(tw_text)83tagger =MeCab.Tagger()84tagger.parse('')85node = tagger.parseToNode(text)86word_list=[]87while node:88 word_type = node.feature.split(',')[0]89 if word_type in ['名詞','代名詞']:90 word_list.append(node.surface)91 node=node.next92word_chain=' '.join(word_list)93 94c=collections.Counter(word_list)95font_path='C:/Windows/Fonts/yuminl.ttf'96words = ['@''RT''https','t','co','プレゼント','し','w','そう', 'ない', 'いる', 'する', 'まま', 'よう', 'てる', 'なる', 'こと', 'もう', 'いい', 'ある', 'ゆく', 'れる', 'ん', 'の']97result = WordCloud(width=800, height=600, background_color='white', 98 font_path=font_path,regexp=r"[\w']+", 99 stopwords=words).generate(word_chain)100result.to_file("./wordcloud_sample1.png")101print(c.most_common(20))102fig = plt.subplots(figsize=(8, 10))103 104sns.set(font="Hiragino Maru Gothic Pro",context="talk",style="white")105sns.countplot(y=word_list,order=[i[0] for i in c.most_common(20)],palette="Blues_r")

コメントを投稿

0 コメント