如何解决无法从 tweepy 中检索推文没有错误/没有输出结果的列
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
输出不显示推文,只显示列标题。我哪里出错了?
解决方法
为了将 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'])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。