【问题标题】:Count number of results for a particular word on Twitter (API v1.1)计算 Twitter 上特定单词的结果数(API v1.1)
【发布时间】:2014-03-02 01:06:43
【问题描述】:

这个问题已经被问过几次了。例如herehere。然而,没有找到可接受的答案。此外,这两个问题都涉及 Twitter API v1.0,它已不再使用。因此,我认为分享我编写的一段简单代码以获取包含给定关键字(或短语)的推文数量可能是有益的。

如果您有任何反馈,请随时回复。

【问题讨论】:

    标签: python twitter python-3.x


    【解决方案1】:

    这里是:

    #Import the required modules
    from twython import Twython
    import json
    import csv
    
    #Set parameters
    keyword = 'kittens'; #The desired keyword(s)
    tweetsXiteration = 100; #Where 100 is the max
    dateFrom = '2014-02-01'; #Inclusive (YYYY-MM-DD)
    dateTo = '2014-02-02'; #Exclusive (YYYY-MM-DD)
    done = False; #Must be false
    
    #Setting the OAuth
    Consumer_Key = 'XXX';
    Consumer_Secret = 'XXX';
    Access_Token = 'XXX';
    Access_Token_Secret = 'XXX';
    
    #Connection established with Twitter API v1.1
    twitter = Twython(Consumer_Key, Consumer_Secret, Access_Token, Access_Token_Secret);
    
    #Twitter is queried
    response = twitter.search(q = keyword, count = tweetsXiteration, since = dateFrom, until = dateTo, result_type = 'mixed');
    
    #Results (partial)
    countTweets = len(response['statuses']);
    
    #If all the tweets have been fetched, then we are done
    if not ('next_results' in response['search_metadata']): 
        done = True;
    
    #If not all the tweets have been fetched, then...
    while (done == False):
    
        #Parsing information for maxID
        parse1 = response['search_metadata']['next_results'].split("&");
        parse2 = parse1[0].split("?max_id=");
        parse3 = parse2[1];
        maxID = parse3;
    
        #Twitter is queried (again, this time with the addition of 'max_id')
        response = twitter.search(q = keyword, count = tweetsXiteration, since = dateFrom, until = dateTo, max_id = maxID, include_entities = 1, result_type = 'mixed');
    
        #Updating the total amount of tweets fetched
        countTweets = countTweets + len(response['statuses']);       
    
        #If all the tweets have been fetched, then we are done
        if not ('next_results' in response['search_metadata']): 
            done = True;
    
    print(countTweets);
    

    请记住:

    1. 您需要通过 OAuth 进行身份验证;
    2. 您只能获取不超过一周的结果;
    3. 如果您想搜索多个单词,如果您只对包含按特定顺序的两个单词的结果感兴趣(例如,'"Stack Overflow"'),则需要使用 ""。

    可以在hereTwitter official documentation 上找到更多信息。

    【讨论】:

    • 这些分号是怎么回事?
    • :)。此外,最好使用 Python 的内置 TrueFalse 值,而不是字符串 'true''false'
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多