【发布时间】:2017-12-23 19:21:53
【问题描述】:
我正在尝试使用 Node js 从 Twitter 获取带有特定主题标签的推文列表。 Twitter 就是这样,每个请求最多只能获得 15 条推文,所以如果我想要一个相当大的推文列表,我需要发出多个请求。为了让 Twitter 知道您想要下一个推文列表,您需要提供一个“max_id”变量,该变量应该保存您从上一个请求中返回的推文列表的最小 id。这个过程记录在here。
这是我的尝试:
var hashtag = "thisisahashtag"; // hashtag that we are looking for
var max_id = '';
do {
twitter.search({
q: "#" + hashtag,
result_type: "recent",
max_id: max_id
},
session.accessToken,
session.accessTokenSecret,
function(error, data, response) { // callback
// get the ids of all the tweets from one response and do comparison for smallest one
for(var i = 0; i < data.statuses.length; i++) {
var id = data.statuses[i].id;
if(max_id == '' || id < parseInt(max_id)) {
max_id = id;
}
}
// do something with the data...
}
)
} while(max_id != '0');
我正在使用 node-twitter-api 模块来发出请求。这是行不通的,因为外部循环将在不等待查询的情况下继续触发。有没有更好的方法来做到这一点?
【问题讨论】: