我有一个Twitter屏幕名称列表,并希望每个屏幕名称收集3200条推文。以下是我从https://gist.github.com/yanofsky/5436496
改编的代码#initialize a list to hold all the tweepy Tweets
alltweets = []
#screen names
r=['user_a', 'user_b', 'user_c']
#saving tweets
writefile=open("tweets.csv", "wb")
w=csv.writer(writefile)
for i in r:
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = i, count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
print "getting tweets before %s" % (oldest)
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = i[0],count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print "...%s tweets downloaded so far" % (len(alltweets))
#write the csv
for tweet in alltweets:
w.writerow([i, tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")])
writefile.close()
最后,最终的csv文件包含3200条针对user_a的推文,大约6400条针对user_b的推文,以及9600条针对user_c的推文。上述代码中的某些内容不正确。每个用户应该有大约3200条推文。任何人都可以指出我的代码有什么问题吗?谢谢。
答案 0 :(得分:1)
由于您使用.extend()
添加到alltweets
,for
循环的每次迭代都会导致所有下一个用户的推文都被添加到上一个推文中。因此,您希望在每个alltweets
循环迭代开始时清除for
:
for i in r:
alltweets = []
...