我上周在Jupyter Notebook上运行了这段代码,并且运行很快。但是,本周我遇到了一个问题,因为它要花很长时间(一个多小时)才能将JSON数据写入文件。该代码有效,但是我很好奇,也许我编写它的方式导致它运行缓慢?
consumer_key = hidden
consumer_secret = hidden
access_token = hidden
access_secret = hidden
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
# set Twitter's rate limit
api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)
# write the querying JSON data into tweet_json.txt
with open('tweet_json.txt','a',encoding = 'utf8') as f:
for tweet_id in twitter_archive['tweet_id']:
try:
tweet = api.get_status(tweet_id, tweet_mode = 'extended') # set mode to extended
json.dump(tweet._json, f)
f.write('\n')
except:
print('error')