1

コードの最初の部分では、ツイートで「ジム」という単語を使用した 20 人のユーザーのリストを取得しています。この部分は正常に動作しています。

2 番目の部分では、最初の部分で取得したユーザー名を使用して、最新の 20 個のツイートをそれぞれ取得しようとしています。

私が現在持っているコードはエラーで実行されていませんが、最初の部分で取得した各人の 20 個のツイートを確実に返していません。最初の部分の結果から最後の行を返すだけです。 .

私のコードは次のとおりです。ご覧のとおり、最初の部分「つぶやき」で作成したリストを 2 番目の部分の id 入力として使用しようとしました。リストの 3 列目 (ユーザー名がある場所)。

import tweepy
from tweepy import OAuthHandler
import pandas as pd

access_token = ''
access_token_secret = ''
consumer_key = ''
consumer_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

tweets = []

count = 20

for tweet in tweepy.Cursor(api.search, q="gym"+'-filter:retweets', since='2020-02-08', tweet_mode='extended',
                           lang='en').items(count):

    try:
        data = [tweet.full_text, tweet.user.screen_name]
        data = tuple(data)
        tweets.append(data)

    except tweepy.TweepError as e:
        print(e.reason)
        continue

    except StopIteration:
        break

df = pd.DataFrame(tweets,
                  columns=['Tweet', '@ Name'])

print(df)

new_tweets = []

username = tweets[1]
count = 20

for user in tweepy.Cursor(api.user_timeline, id=username, tweet_mode='extended').items(count):

    try:
        data = [tweet.full_text, tweet.user.screen_name]
        data = tuple(data)
        new_tweets.append(data)

    except tweepy.TweepError as e:
        print(e.reason)
        continue

    except StopIteration:
        break

df2 = pd.DataFrame(new_tweets, columns=['Tweets', '@ Name'])

print(df2)

df2.to_csv('test3.csv')

そして、これは私の出力です:

                                                Tweet          @ Name
0            Gym chronicles                             chocodilish
1   @neilmcrowther @SpotifyUK I have a Spotify pla...    carey_bamber
2   Food pick-up for virtual learners today 9:00-1...    allentrotter
3                         couldn’t sleep so gym it is   esmeraldahdz_
4   We need I.D. to buy beer, to buy ciggies, we n...       beryl1946
5   So I actually have to go to the gym to have a ...       ___tshego
6   Currently three Marcela Bielsa lookalikes in t...     sammyptweet
7   I’m dreading going to the gym and coming back ...   cinnamonKayyy
8   yes we were there... what the fuck is going on...        blubbsie
9   @IamEzeNwanyi @LilburnEnugu @mr_robmichael @He...        _lilivet
10                                   GYM WEEK 2  LEGO        Mondo_92
11  Webinars for this week are as follows,\nBrain ...    EdCentreMayo
12    I rather be wakin up for the gym than work tbh.    illmindofPAT
13  First day back in the gym doing BASKETBALL  ...  AUMWarhawksWBB
14  i don’t wanna go to school today since i know ...  CEOofTsuyuAsui
15  @sunikies GYM DHSHSKDSH (i miss it :( ), indiv...       shienIove
16  @PaulMumba_ Is that gym work I'm seeing on tha...       jaymaxgie
17  Body builders on Instagram don’t go to the gym...  OfficialShann_
18                      @DivinePooh gym and game room     FinesseDee2
19  I use to wake up to go to the gym at this hour...    missgenafire
20
                                              Tweets        @ Name
0  I use to wake up to go to the gym at this hour...  missgenafire

Process finished with exit code 0

どんな助けでも大歓迎です、どうもありがとう。

4

2 に答える 2