【问题标题】:Unable to retrieve tweets from tweepy (No error/columns with no result in output)无法从 tweepy 检索推文(无错误/无输出结果的列)
【发布时间】:2021-04-21 18:13:51
【问题描述】:
apikey = '2238c8h8E25gSVU1WW28ti7fS7'
apisecretkey = 'ssLG9s4rt4QwLo6PFyMSpLVRT1IoQ3f1EwrrgzTg6TRJLUTeI5e'
accesstoken = '33347844103627698178-3HuOoCCFuMWHwLTmhswKUtJSvG22et'
accesstokensecret = '2s8tAcatrjTHgh81Oo7dw6rvWGGRFZoSrPDa5eInY22Q3c'

auth = tw.OAuthHandler(apikey,apisecretkey) #calling OAuthHandler required for authantication with Twitter
auth.set_access_token(accesstoken,accesstokensecret)

api = tw.API(auth,wait_on_rate_limit=True)

search_word = '#IndvsAus' or '#AusvsInd'
date_since = '2021-01-10'
date_until = '2021-01-11'

tweets = tw.Cursor(api.search,q = search_word+' -filter:retweets',\
                   lang ='en',tweet_mode='extended',since='date_since',until='date_until').items(100)

tweet_details = [[tweet.id,tweet.source,tweet.full_text,tweet.user.location,tweet.user.created_at,tweet.user.verified,tweet.created_at]for tweet in tweets]

import pandas as pd
tweet100_df = pd.DataFrame(data = tweet_details,columns=['tweet_id','source','Full_text','User_location','User_created_at','User_verified','tweet_timestamp',])
pd.set_option('max_colwidth',800)
tweet100_df.head(20)

输出:tweet_id source Full_text User_location User_created_at User_verified tweet_timestamp

输出没有显示推文,只有列标题。我哪里错了?

【问题讨论】:

    标签: python-3.x pandas twitter tweepy sentiment-analysis


    【解决方案1】:

    为了将 Tweepy 的 Cursor API 的输出转储到 Pandas DataFrame 中,您需要传递 pd.DataFrame 一个字典列表和您感兴趣的字段作为列名。

    Tweepy 具有将 Cursor items() 方法中的数据结构化到字典中的方法。

    在你的情况下:

    tweets = tw.Cursor(api.search,q = search_word+' -filter:retweets',\
                       lang ='en',tweet_mode='extended',since='date_since',until='date_until').items(100)
    
    
    list_of_dicts = []
    for each_json_tweet in tweets:
        list_of_dicts.append(tweets._json)
    

    然后你可以这样做:

    tweet100_df = pd.DataFrame(data=list_of_dicts,columns=['tweet_id','source','Full_text','User_location','User_created_at','User_verified','tweet_timestamp'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2018-02-25
      • 1970-01-01
      • 2014-08-20
      相关资源
      最近更新 更多