问题描述
我正在尝试针对给定的句柄返回200条以上的推文。 每个句柄最多可以接收200条推文,但是对于给定的Twitter句柄,设置光标/浏览较旧的推文有困难。 这是我的代码:
import tweepy
import pandas as pd
import numpy as np
consumerKey,consumerSecret,accesstoken,accesstokenSecret = 'x','x','x'
authenticate = tweepy.OAuthHandler(consumerKey,consumerSecret)
authenticate.set_access_token(accesstoken,accesstokenSecret)
api_twitter = tweepy.API(authenticate,wait_on_rate_limit=True)
def get_tweets(handle):
try:
tweets = api_twitter.user_timeline(screen_name=handle,count=200,exclude_replies=True,include_rts=False,lang="en",tweet_mode="extended")
print(handle,"Number of tweets extracted: {}\n".format(len(tweets)))
df = pd.DataFrame(data=[tweet.user.screen_name for tweet in tweets],columns=['Handle'])
df['Tweets'] = np.array([tweet.full_text for tweet in tweets])
df['Date'] = np.array([tweet.created_at - timedelta(hours=4) for tweet in tweets])
df['Len'] = np.array([len(tweet.full_text) for tweet in tweets])
df['Like_count'] = np.array([tweet.favorite_count for tweet in tweets])
df['RT_count'] = np.array([tweet.retweet_count for tweet in tweets])
except:
pass
return df
df = pd.DataFrame()
for handle in handles:
df_new = get_tweets(handle)
df = pd.concat((df,df_new))
我尝试以以下方式编辑函数无济于事(什么都没有发生):
def get_tweets(handle):
try:
tweets = api_twitter.user_timeline(screen_name=handle,columns=['Handle'])
df['Tweets'] = np.array([tweet.full_text for tweet in tweets])
df['Date'] = np.array([tweet.created_at - timedelta(hours=4) for tweet in tweets])
df['Len'] = np.array([len(tweet.full_text) for tweet in tweets])
df['Like_count'] = np.array([tweet.favorite_count for tweet in tweets])
df['RT_count'] = np.array([tweet.retweet_count for tweet in tweets])
# ### TRYING TO GET MORE THAN 200 TWEETS FOR EACH HANDLE
df = df.extend(tweets)
oldest = df[-1].id - 1
while len(tweets) > 0:
print(f"getting tweets before {oldest}")
tweets = api_twitter.user_timeline(screen_name=handle,max_id=oldest)
df = df.extend(tweets)
oldest = df[-1].id - 1
print(f"...{len(df)} DOWNLOADED TWEETS COUNT")
# ###
except:
pass
return df
实现此请求的最佳方法是在哪里? 预先谢谢你。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)