23
19

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トレンドとGoogleトレンドから情報を取得[python3.x]

Last updated at Posted at 2019-06-17

最終更新:2019年6月17日

google trend情報取得

本記事上の変数の定義

変数 内容
pytrends google trendとのAPI接続
trend 日本のgoogle trend取得
trend2 リストにする
Google_list googleトレンドのリスト
Twitter_list twitterトレンドのリスト

コーディング指定


!/usr/bin/env python
-*- coding: utf-8 -*-

pytrendsライブラリのインストール


*pip install pytrends*

各種モジュールのインポート


from pytrends.request import TrendReq
import pandas, numpy

日本のgoogleトレンド取得


pytrends = TrendReq()
trend = pytrends.trending_searches(pn = 'japan')

pandas.dataframe形式での取得なので、リスト形式に変換(必要あれば)


trend2 = trend.values.tolist()

単一リストにして扱いやすく


Google_trend_list = []
for i in trend2:
    Google_trend_list.extend(i)

print(Google_trend_list)

参考

Twitter trend情報取得

twitter apiと、twitter認証のrequestsのインストール


*pip install twitter*
*pip install requests requests-oauthlib*

各種モジュールのインポート。myconfigという同ディレクトリ内の別ファイルにtwitterAPI情報記載、インポート。


from twitter import *
import re
import myconfig

Twitter認証


CK = myconfig.Consumer_Key
CS = myconfig.Consumer_Secret_Key
AT = myconfig.Access_Token
AS = myconfig.Access_Secret_Token

twitter = Twitter(auth = OAuth(AT,AS,CK,CS))

idは地域IDにおける日本(2342856)を指定


results = twitter.trends.place(_id = 23424856)

取得したtwitterリストの’#’を消し、リスト形式にする


Pre_Twitter_list = []
for location in results:
        for trend in location["trends"]:
                Pre_Twitter_list.append(re.sub('#', "", trend["name"]))

必要ならば、Twitter trendのcountをGoogleのcountに合わせる


Twitter_list = Pre_Twitter_list[:len(Google_list)]

print(Twitter_list)
23
19
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
23
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?