指定したユーザーのつぶやきに、指定したキーワードを使用してツイートテキストに照会しようとしました。ここに私のコードは次のとおりです。PythonとTweepyを使用してTwitterのステータスを照会
# Import Tweepy, sleep, credentials.py
import tweepy
from time import sleep
from credentials import *
# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
SCREEN_NAME = "BachelorABC"
KEYWORD = "TheBachelor"
def twtr2():
raw_tweets = tweepy.Cursor(api.search, q=KEYWORD, lang="en").items(50)
for tweet in raw_tweets:
if tweet['user']['screen_name'] == SCREEN_NAME:
print tweet
twtr2()
以下のように私は、エラーメッセージが表示されます:私は多くのことをGoogleで検索し、多分私が最初にPythonでTwitterのJSONを保存する必要があると考え、私は次のことを試してみました
Traceback (most recent call last):
File "test2.py", line 19, in <module>
twtr2()
File "test2.py", line 17, in twtr2
if tweet['user']['screen_name'] == SCREEN_NAME:
TypeError: 'Status' object has no attribute '__getitem__'
:
import tweepy, json
from time import sleep
from credentials import *
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
SCREEN_NAME = "BachelorABC"
KEYWORD = "TheBachelor"
raw_tweets = tweepy.Cursor(api.search, q=KEYWORD, lang="en").items(50)
for tweet in raw_tweets:
load_tweet = json.loads(tweet)
if load_tweet['user']['screen_name'] == SCREEN_NAME:
print tweet
しかし、結果は悲しいです:
Traceback (most recent call last):
File "test2.py", line 35, in <module>
load_tweet = json.loads(tweet)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
誰かが私のコードに何が間違っているか知っていますか?そしてそれを修正するために私を助けることができますか?
ありがとうございます!
ありがとうございます。@ethanchewy **指定された** screen_nameとキーワードでつぶやきを探しています。あなたの答えは一般的に最初の20のつぶやきについてです。あなたはこれらの条件でつぶやきを見つける考えがありますか? – Counter10000
@LinguisticsStudent https://github.com/tweepy/tweepy/blob/master/docs/code_snippet.rstにある最後のコードスニペットをご覧ください。あなたはリストにscreen_namesを格納し、特定のscreen_nameのためにそのリスト内を検索します。 Twitterにはクエリに厳しい制限があることに注意してください。ありがとう@ethanchewy。 – ethanchewy
上記で引用したページは、statusではなく、followerまたはuserからscreen_nameを取得することです。私は後で答えが見つかると更新します。 – Counter10000