【发布时间】:2017-12-14 14:55:17
【问题描述】:
我对编码很陌生,但遇到了一些问题。我想在 Twitter 中显示某些特定用户的追随者......的追随者。我已经对此进行了编码,并且可以设置深度限制。但是,在使用小样本运行代码时,我发现我再次遇到了相同的用户,并且我的代码重新显示了这些用户的关注者。我怎样才能避免这种情况并跳到下一个用户?您可以在下面找到我的代码:
顺便说一句,在运行我的代码时,我遇到了 401 错误。在我正在处理的列表中,有一个私人用户,当我的代码捕获该用户时,它就会停止。另外,我该如何处理这个问题?我想跳过这些用户并阻止我的代码停止。
提前感谢您的帮助!
PS:我知道在处理大样本时会遇到 429 错误。解决这些问题后,我计划回顾相关讨论以处理。
public class mainJava {
public static Twitter twitter = buildConfiguration.getTwitter();
public static void main(String[] args) throws Exception {
ArrayList<String> rootUserIDs = new ArrayList<String>();
Scanner s = new Scanner(new File("C:\\Users\\ecemb\\Desktop\\rootusers1.txt"));
while (s.hasNextLine()) {
rootUserIDs.add(s.nextLine());
}
s.close();
for (String rootUserID : rootUserIDs) {
User rootUser = twitter.showUser(rootUserID);
List<User> userList = getFollowers(rootUser, 0);
}
}
public static List<User> getFollowers(User parent, int depth) throws Exception {
List<User> userList = new ArrayList<User>();
if (depth == 2) {
return userList;
}
IDs followerIDs = twitter.getFollowersIDs(parent.getScreenName(), -1);
long[] ids = followerIDs.getIDs();
for (long id : ids) {
twitter4j.User child = twitter.showUser(id);
userList.add(child);
getFollowers(child, depth + 1);
System.out.println(depth + "th user: " + parent.getScreenName() + " Follower: " + child.getScreenName());
}
return userList;
}
}
【问题讨论】:
标签: api twitter duplicates twitter4j