我正在尝试获取关于谁跟踪谁基于已关闭的特定用户后跟的Twitter帐户列表的网络数据。也就是说,鉴于用户A ,我想检索其好友列表,然后了解其好友中的哪些人互相关注。
我遇到的第一个问题是Twitter API设置的速率限制,但似乎使用 Sys.sleep()函数来解决它。虽然非常慢,但我使用了下面显示的功能,并且第一次工作正常。但是,我试图从其他用户获得相同的信息'朋友列表,它不断给我错误的类型:
提前多多感谢!
friendsnet <- function(tuser) {
require(twitteR)
# if rate limit is hit, wait for 15 minutes
limit <- getCurRateLimitInfo()[53,3]
print(paste("Look up limit", limit))
if (limit == 0) {
print("sleeping for fifteen minutes")
Sys.sleep(900)
}
# Find user
tuser <- getUser(tuser)
print(tuser$screenName)
# Empty dataframe
df <- NULL
print("empty data frame")
# Get names of friends
f <- lookupUsers(tuser$getFriendIDs())
f.id <- sapply(f, id)
f.name <- sapply(f, screenName)
f2 <- as.data.frame(cbind(f.id, f.name))
print("list of friends")
print(head(f2))
for (i in f2$f.name) {
# if rate limit is hit, wait for 15 minutes
limit <- getCurRateLimitInfo()[53,3]
print(paste("Look up limit", limit))
if (limit == 0) {
print("sleeping for fifteen minutes")
Sys.sleep(900)
}
A <- getUser(i)
friends.object <- lookupUsers(A$getFriendIDs())
# Convert list into data frame
friends.id <- sapply(friends.object,id)
friends.name <- sapply(friends.object, screenName)
friends <- as.data.frame(cbind(friends.id, friends.name))
for (j in f2$f.name) {
if (i != j) {
if ((j %in% friends$friends.name) == TRUE) {
print(paste(i, "follows", j))
df <- rbind(df, data.frame(i, j))
}
}
}
}
return(df)
}