取得には、WOEID(Yahoo! Where On Earth ID)が必要になります。
取得は trends_placeメソッドにWOEIDを指定しますが、決められたWOEIDしか受け付けないようです。
指定できるWOEIDを取得するには trends_availableメソッドを使用します。
サンプルでは、これらを組み合わせてトレンドを取得し、ブラウザでトレンドをキーワード検索させてみます。
import tweepy
import urllib
import webbrowser
CONSUMER_KEY = '(コンシューマキー)'
CONSUMER_SECRET = '(コンシューマシークレット)'
ACCESSS_TOKEN = '(アクセストークン)'
ACCESS_SECRET = '(アクセストークン シークレット)'
class myExeption(Exception):
pass
def InputNum(min=0, max=99):
tmp = input('No ? ')
if tmp.isnumeric() == False:
return None
num = int(tmp)
return num if (num>= min) and (num <= max) else None
if __name__ == '__main__':
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESSS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
woeid = []
idx = 0;
print('----- Area -----')
for topic in api.trends_available():
if topic['country'].lower() =='japan':
woeid.append(topic['woeid'])
print("{0}:{1}".format(idx, topic['name']))
idx += 1
try:
no = InputNum(max=len(woeid)-1)
if no == None:
raise myExeption
idx = 0
place = api.trends_place(id=woeid[no])[0]
print('----- Trend -----')
for trend in place['trends']:
print( '{0}:{1}'.format( idx, urllib.parse.unquote( trend['query'] ) ) )
idx += 1
no = InputNum(max=len(place['trends'])-1)
if no == None:
raise myExeption
trend = place['trends'][no]
webbrowser.open(trend['url'])
except:
print('Error.')
実行すると、コンソールにエリア一覧が表示されます。
エリアを指定すると、そのエリアでのトレンドワードが10件表示されますので、同じく番号で指定しま
す。
その後、ブラウザが起動し、トレンドワードの検索結果が表示されます。

