【问题标题】:Extracting 5 years of twitter data using an API使用 API 提取 5 年的 Twitter 数据
【发布时间】: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


【解决方案1】:

您在此处使用的方法是点击 Twitter 用户时间线端点。此 API 仅支持从单个用户检索最多 3200 条最新推文。

您列出的所有帐户都是高流量媒体帐户,迄今为止发布了数十万条推文,因此您在结果集中看到任何 2017 年的推文的可能性非常小。

要检索该时间段的推文,您需要使用Twitter's full archive search API,这是一个具有有限免费层的高级 API。数量可能会很高。

【讨论】:

  • 好的,我会浏览文档。限速有时间限制吗?我的意思是 3200 条推文 15 分钟?我问是因为那时我可能会插入睡眠 15 分钟。可能这不起作用,因为它总是从当前时间开始搜索。最好通过文档,谢谢
  • 时间线上可用推文的最大总数是 3200 条推文,从现在开始倒退。每个请求最多可以请求 200 条推文。每 15 分钟窗口最多可以发出 900 个请求。 24 小时内的总通话次数上限为 100,000 次。对于您在此处尝试执行的操作,使用时间线 API 不是正确的解决方案。您需要使用完整的存档搜索。
猜你喜欢
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2013-02-23
  • 2015-08-13
相关资源
最近更新 更多