请帮助,我对此非常陌生,需要为西南航空收集推文。我做错了什么?什么&如果我想要一个特定的公司名称,我该在哪里写?谢谢。
line 32, in get_all_tweets
oldest = alltweets[-1].id - 1
IndexError: list index out of range
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
alltweets = []
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
alltweets.extend(new_tweets)
oldest = alltweets[-1].id - 1
while len(new_tweets) > 0:
print "getting tweets before %s" % (oldest)
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
alltweets.extend(new_tweets)
oldest = alltweets[-1].id - 1
print "...%s tweets downloaded so far" % (len(alltweets))
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
with open('%s_tweets.csv' % screen_name, 'wb') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("Tverichanka"
)
答案 0 :(得分:1)
此错误清楚地表明您的列表alltweets
为空。这意味着api.user_timeline(screen_name = screen_name,count=200)
的返回值为[]
。
可能是因为一些不同的问题。其中一个可能是您使用的screen_name
有拼写错误。
答案 1 :(得分:0)
打印new_tweets
并查看您获得的内容。您的错误很可能是由于没有找到new_tweets
,您实际上是在下载数据吗?否则,您将尝试获取空列表的最后一个元素,并获得该错误。
alltweets = []
alltweets.extend([]) # if new_tweets is empty will cause alltweets to remain empty
oldest = alltweets[-1].id - 1 # can't get the last element of an empty list.
哦,我正在谈论while
循环之前的那一行。