【发布时间】:2021-03-25 05:35:17
【问题描述】:
我正在尝试从 2012-01-01 到 2018-12-31 抓取 7 年的数据。我正在使用 tweepy,我有以下代码
usernames = ["CNBC","MarketWatch","verge","YahooFinance"]
api = tweepy.API(auth)
start_date = datetime.datetime(2012,1,1,0,0,0)
end_date = datetime.datetime(2017,12,31,0,0,0)
def create_dictionary(username="",tweet_id="",time="",text="",retweet_count=0,favourite_count=0):
return {
"USERNAME": username,
"TWEET_ID": tweet_id,
"TIME": time,
"TWEET": text,
"RETWEET_COUNT":retweet_count,
"FAVOURITE_COUNT":favourite_count
}
tweet_id = []
time = []
tweet = []
rt_count = []
fav_count = []
for i,username in enumerate(usernames):
print("Scraping for {}".format(username))
for status in tweepy.Cursor(api.user_timeline,id=username).items():
print(f'Last status had timestamp @ {status.created_at}')
if status.created_at < start_date:
break
if (status.created_at >= start_date and status.created_at <= end_date) :
tweet_id.append(str(status.id))
time.append(str(status.created_at))
tweet.append(status.text)
rt_count.append(status.retweet_count)
fav_count.append(status.favorite_count)
dictionary = [
create_dictionary(username=username,
tweet_id = val[0],time=val[1],text=val[2],retweet_count=val[3],favourite_count=val[4])
for val in zip(tweet_id,time,tweet,rt_count,fav_count)
]
clear_output(wait=True)
try:
print("Going for the next username {}".format(usernames[i+1]))
except:
print("Done")
pass
with open('training_tweets.json', 'a') as fp:
json.dump(dictionary, fp,indent=4)
什么都没有被抓取,它移动到下一个用户名,[] 被转储到 json 文件中。
是否有速率限制,是否有其他 API 可以抓取历史推特数据?
【问题讨论】:
-
请将错误和完整的堆栈跟踪添加到问题中
-
没有错误,scraper 抓取失败,移动到下一个用户名。只有 [] 被转储到 json 文件中
-
“但我遇到了超时错误”。所以你没有超时错误。
-
不,我没有超时,它只是移动到下一个用户名直到它到达末尾。
-
如果您的代码使用过去 12 个月内的开始日期和结束日期可以正常工作,那么这是 API 本身的限制。
标签: python api web-scraping twitter twitter-oauth