我想从tweepy中获取推文作者,然后出于特定目的将它们从一个列表放到另一个列表中,但是当我使用此代码时,“ allnames”列表变量返回零。请查看代码结尾并为我提供帮助。
OAUTH_KEYS = {'consumer_key':consumer_key,
'consumer_secret':consumer_secret,
'access_token_key':access_key, 'access_token_secret':access_secret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'],
OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
search = tweepy.Cursor(api.search, q='#x').items(100)
# Create lists for each field desired from the tweets.
sn = []
text = []
timestamp =[]
for tweet in search:
#print tweet.user.screen_name, tweet.created_at, tweet.text
timestamp.append(tweet.created_at)
sn.append(tweet.user.screen_name)
text.append(tweet.text)
# Convert lists to dataframe
df = pd.DataFrame()
df['timestamp'] = timestamp
df['sn'] = sn
df['text'] = text
# Prepare ford date filtering. Adding an EST time column since chat
#hosted by people in that time zone.
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['EST'] = df['timestamp'] - pd.Timedelta(hours=5) #Convert to EST
df['EST'] = pd.to_datetime(df['EST'])
# Subset for the dates required. Can select a specific date or time to
examine.
df = df[(pd.to_datetime("2018-12-14 20:00:00", format='%Y-%m-%d
%H:%M:%S') < df['EST']) & (df['EST'] < pd.to_datetime("2018-12-14
21:00:00", format='%Y-%m-%d %H:%M:%S'))]
# Write out Tweets in case they are needed later.(it doesn't write that
#file!)
df.to_csv('x.csv',index = False,encoding='utf-8')
# Create a list of the unique usernames in order to see which users we
#need to retrieve friends for(here is the problem!).
allNames = list(df['sn'].unique())