【问题标题】:Get all twitter mentions using tweepy for users with Millions of followers使用 tweepy 为拥有数百万追随者的用户获取所有 Twitter 提及
【发布时间】:2016-11-28 01:18:02
【问题描述】:

我有一个项目,我将下载过去一年发送给名人的所有推文,并对它们进行情绪分析,并评估谁拥有最积极的粉丝。

然后我发现你最多可以使用 tweepy/twitter API 检索过去 7 天的 twitter 提及。我翻遍了网络,但在过去一年中找不到任何下载推文的方法。

无论如何,我决定只对最近 7 天的数据做这个项目,并编写了以下代码:

try:
    while 1:
        for results in tweepy.Cursor(twitter_api.search, q="@celebrity_handle").items(9999999):
            item = (results.text).encode('utf-8').strip()
            wr.writerow([item, results.created_at])  # write to a csv (tweet, date)

我正在使用 Cursor 搜索 api,因为 other 获取提及的方式(更准确的方式)具有仅检索最后 800 条推文的限制。

无论如何,在运行代码一夜之后,我只能下载 32K 的推文。其中大约 90% 是转推。

有没有更好更有效的方法来获取提及数据?

请记住:

  1. 我想为多个名人这样做。 (著名的有 百万追随者)。
  2. 我不在乎转发。
  3. 他们每天向他们发送数千条推文。

欢迎提出任何建议,但目前我没有想法。

【问题讨论】:

    标签: python twitter tweepy sentiment-analysis


    【解决方案1】:

    我会使用搜索 API。我用下面的代码做了类似的事情。它似乎完全按预期工作。我在一个特定的电影明星身上使用了它,并拉出了 15568 条推文,在快速扫描后,所有这些似乎都是@提及它们。 (我从他们的整个时间线中提取出来。)

    在您的情况下,在您希望每天运行的搜索中,我会存储您为每个用户提取的最后一次提及的 id,并在每次重新运行时将该值设置为“sinceId”搜索。

    顺便说一句,AppAuthHandler 比 OAuthHandler 快得多,而且此类数据拉取不需要用户身份验证。

    auth = tweepy.AppAuthHandler(consumer_token, consumer_secret)
    auth.secure = True
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
    

    searchQuery = '@username' 这就是我们要搜索的内容。在您的情况下,我将创建一个列表并遍历搜索查询运行的每次传递中的所有用户名。

    retweet_filter='-filter:retweets' 这会过滤掉转发

    在下面的每个 api.search 调用中,我会将以下内容作为查询参数放入:

    q=searchQuery+retweet_filter
    

    以下代码(以及上面的 api 设置)来自this link

    tweetsPerQry = 100 # 这是 API 允许的最大值

    fName = 'tweets.txt' # 我们会将推文存储在一个文本文件中。

    如果需要从特定 ID 开始的结果,请将 sinceId 设置为该 ID。 否则默认无下限,只要 API 允许就回溯

    sinceId = None
    

    如果结果仅低于特定 ID,请将 max_id 设置为该 ID。 否则默认无上限,从匹配搜索查询的最新推文开始。

    max_id = -1L
    //however many you want to limit your collection to.  how much storage space do you have?
    maxTweets = 10000000 
    
    tweetCount = 0
    print("Downloading max {0} tweets".format(maxTweets))
    with open(fName, 'w') as f:
        while tweetCount < maxTweets:
            try:
                if (max_id <= 0):
                    if (not sinceId):
                        new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
                    else:
                        new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                                since_id=sinceId)
                else:
                    if (not sinceId):
                        new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                                max_id=str(max_id - 1))
                    else:
                        new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                                max_id=str(max_id - 1),
                                                since_id=sinceId)
                if not new_tweets:
                    print("No more tweets found")
                    break
                for tweet in new_tweets:
                    f.write(jsonpickle.encode(tweet._json, unpicklable=False) +
                            '\n')
                tweetCount += len(new_tweets)
                print("Downloaded {0} tweets".format(tweetCount))
                max_id = new_tweets[-1].id
            except tweepy.TweepError as e:
                # Just exit if any error
                print("some error : " + str(e))
                break
    
    print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
    

    【讨论】:

    • 可能是一个愚蠢的查询,但你在哪里定义了 maxTweets?
    猜你喜欢
    • 2020-02-20
    • 2018-08-20
    • 1970-01-01
    • 2015-01-03
    • 2013-06-30
    • 2015-10-08
    • 2014-01-03
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多