计算 Twitter 上特定单词的结果数(API v1.1) [英] Count number of results for a particular word on Twitter (API v1.1)
本文介绍了计算 Twitter 上特定单词的结果数(API v1.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题已经被问过几次了.例如此处或此处.然而,没有找到公认的答案.此外,这两个问题都涉及不再使用的 Twitter API v1.0.因此,我认为分享我编写的一段简单代码可能会有所帮助,以便获得包含给定关键字(或短语)的推文数量.
The question has already been asked a couple of times. For example here or here. Yet, no accepted answer was found. Also, both the questions refer to Twitter API v1.0, which is no longer in use. Thus, I thought it could be beneficial to share a simple piece of code I wrote in order to have the number of tweets containing a given keyword (or phrase).
如果您有任何反馈,请随时回复.
If you have any feedbacks, do not hesitate to reply.
推荐答案
这里是:
#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);
请记住:
- 您需要通过 OAuth 进行身份验证;
- 您只能获取不超过一周的结果;
- 如果您想搜索多个单词,如果您只对包含该特定顺序的两个单词的结果感兴趣,则需要使用 ""(例如,'"Stack Overflow"').
其他信息可在此处或Twitter 官方文档.
这篇关于计算 Twitter 上特定单词的结果数(API v1.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文