2014-02-05 42 views
4

該問題已被問及幾次。例如herehere。然而,沒有找到可以接受的答案。另外,這兩個問題都涉及Twitter API v1.0,該API已不再使用。因此,我認爲分享我寫的一段簡單的代碼以獲得包含給定關鍵字(或短語)的推文數量可能是有益的。計算Twitter上特定單詞的結果數(API v1.1)

如果您有任何反饋意見,請不要猶豫,回覆。

回答

6

這就是:

#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

+0

這些分號有什麼關係? –

+1

作爲MATLAB程序員太多年了,對不起! –

+0

:)。另外,使用Python內建的'True'和'False'值可能是一個好主意,而不是字符串'true'和'false'。 –