例1-3 Twitterの検索トレンドの取得
import twitter
twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
trends = twitter_api.trends()
[trend['name'] for trend in trends['trends']]
の4行目が,できない.
参考 Iyken: 【Python】入門ソーシャルデータ
Iyken: 【Python】入門ソーシャルデータ その2
修正点は2つ.
1箇所め.
エラーメッセージ:
"twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/trends.json using parameters: ()"
URLが違うそうです.
↓
api.pyの該当箇所を書き直す.
(略)
def __call__(self, **kwargs):
# Build the uri.
uriparts = []
for uripart in self.uriparts:
# If this part matches a keyword argument, use the
# supplied value otherwise, just use the part.
uriparts.append(str(kwargs.pop(uripart, uripart)))
uri = '/'.join(uriparts)
if self.domain == "api.twitter.com": # ←ココと
uri = uri + '/1' # ←ココ!
(略)
2箇所め.
エラーメッセージ:
"TypeError: list indices must be integers, not str"
↓
4行目のリスト内包表記の部分を
[trend['name'] for trend in trends[0]["trends"]]
と書き換える.
これで成功しました.
ちなみに現在のトレンドはこちら.
[u'2 Chainz', u'#WorstTimesToGetABoner', u'#WhatGetsMeMad', u'#takeoverUK', u'BTW Ball Will Dominate The World', u'Prabhu Deva', u"Ben & Jerry's", u'Samantha Brick', u'BSkyB', u'James Murdoch']
ありがとうございます! これでトレンドが取得できました!
返信削除