我正在编写一个Python代码来从Twitter帐户中提取推文。我现在遇到了一些麻烦。
下面是我的代码(我删除了我的cosumer和访问ID):
import csv
import tweepy
from tweepy import OAuthHandler
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
def get_all_tweets(screen_name):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, 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__':
get_all_tweets("hello")
当我运行它时,我收到此错误:
有人知道我哪里出错吗?
答案 0 :(得分:0)
只需关闭您要写的打开的文件。
这是 hello_tweets.csv
答案 1 :(得分:0)
检查您是否有权打开该文件以及在该文件夹中读/写的权限。
我不推荐它,但是如果您需要运行代码并且找不到问题,请尝试以管理员身份执行。