twitteR 如何搜索两个主题标签
twitteR how to search for two hashtags
在此处输入代码是否可以在一个 searchTwitter
命令中查找两个不同的 hastags?
示例
my_h <- as.POSIXlt(Sys.time())
my_h <- strptime(my_h, format = "%Y-%m-%d %H:%M:%S", tz = "CET")
hrs <- function(u) {
x <- u * 3600
return(x)
}
my_h <- my_h - hrs(24)
my_h <- data.frame(day = strptime(my_h, "%Y-%m-%d", tz = ""))
我想寻找 hastags #dn
和 #park
我可以单独做如下
tweets<-twListToDF(searchTwitter("#dn", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_dn_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
tweets<-twListToDF(searchTwitter("#park", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_park_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
问题是这两个能不能合二为一?
尝试这样的事情
hashtags <- c("#metallica", "#slayer")
needle <- paste(hashtags, collapse = " OR ")
tweets <- searchTwitter(needle, n = 10)
df <- twListToDF(tweets)
for (hashtag in hashtags) {
write.csv(df[grep(hashtag, tolower(df$text), fixed = TRUE), ], paste0(hashtag, ".csv"))
}
在推文上使用 tolower
可能需要一些错误处理 - 您会在网络上找到大量相关信息。
我发现发布 here 的答案更优雅。对于目前的情况,它基本上是
hashtags <- '#metallica + #slayer'
tweets <- searchTwitter(hashtags, n = 10, lang = 'en', retryOnRateLimit = 100)
tweetsDF <- twListToDF(tweets)
在此处输入代码是否可以在一个 searchTwitter
命令中查找两个不同的 hastags?
示例
my_h <- as.POSIXlt(Sys.time())
my_h <- strptime(my_h, format = "%Y-%m-%d %H:%M:%S", tz = "CET")
hrs <- function(u) {
x <- u * 3600
return(x)
}
my_h <- my_h - hrs(24)
my_h <- data.frame(day = strptime(my_h, "%Y-%m-%d", tz = ""))
我想寻找 hastags #dn
和 #park
我可以单独做如下
tweets<-twListToDF(searchTwitter("#dn", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_dn_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
tweets<-twListToDF(searchTwitter("#park", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_park_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
问题是这两个能不能合二为一?
尝试这样的事情
hashtags <- c("#metallica", "#slayer")
needle <- paste(hashtags, collapse = " OR ")
tweets <- searchTwitter(needle, n = 10)
df <- twListToDF(tweets)
for (hashtag in hashtags) {
write.csv(df[grep(hashtag, tolower(df$text), fixed = TRUE), ], paste0(hashtag, ".csv"))
}
在推文上使用 tolower
可能需要一些错误处理 - 您会在网络上找到大量相关信息。
我发现发布 here 的答案更优雅。对于目前的情况,它基本上是
hashtags <- '#metallica + #slayer'
tweets <- searchTwitter(hashtags, n = 10, lang = 'en', retryOnRateLimit = 100)
tweetsDF <- twListToDF(tweets)