Ger*_*ert 5 xml twitter loops r
我创建了一个循环函数,使用搜索 api 以一定的时间间隔(假设每 5 分钟)提取推文。该函数执行其预期的操作:连接到 Twitter,提取包含特定关键字的推文,并将其保存在 csv 文件中。然而,偶尔(一天 2-3 次)循环会由于以下两个错误之一而停止:
htmlTreeParse(URL, useInternal = TRUE) 中的错误:为http://search.twitter.com/search.atom?q= 6.95322e-310tst&rpp=100&page=10创建解析器时出错
UseMethod("xmlNamespaceDefinitions") 中的错误:没有适用于“xmlNamespaceDefinitions”的方法应用于类“NULL”的对象
我希望您能通过回答我的一些问题来帮助我处理这些错误:
我的函数(基于网上找到的几个脚本)如下:
library(XML) # htmlTreeParse
twitter.search <- "Keyword"
QUERY <- URLencode(twitter.search)
# Set time loop (in seconds)
d_time = 300
number_of_times = 3000
for(i in 1:number_of_times){
tweets <- NULL
tweet.count <- 0
page <- 1
read.more <- TRUE
while (read.more)
{
# construct Twitter search URL
URL <- paste('http://search.twitter.com/search.atom?q=',QUERY,'&rpp=100&page=', page, sep='')
# fetch remote URL and parse
XML <- htmlTreeParse(URL, useInternal=TRUE, error = function(...){})
# Extract list of "entry" nodes
entry <- getNodeSet(XML, "//entry")
read.more <- (length(entry) > 0)
if (read.more)
{
for (i in 1:length(entry))
{
subdoc <- xmlDoc(entry[[i]]) # put entry in separate object to manipulate
published <- unlist(xpathApply(subdoc, "//published", xmlValue))
published <- gsub("Z"," ", gsub("T"," ",published) )
# Convert from GMT to central time
time.gmt <- as.POSIXct(published,"GMT")
local.time <- format(time.gmt, tz="Europe/Amsterdam")
title <- unlist(xpathApply(subdoc, "//title", xmlValue))
author <- unlist(xpathApply(subdoc, "//author/name", xmlValue))
tweet <- paste(local.time, " @", author, ": ", title, sep="")
entry.frame <- data.frame(tweet, author, local.time, stringsAsFactors=FALSE)
tweet.count <- tweet.count + 1
rownames(entry.frame) <- tweet.count
tweets <- rbind(tweets, entry.frame)
}
page <- page + 1
read.more <- (page <= 15) # Seems to be 15 page limit
}
}
names(tweets)
# top 15 tweeters
#sort(table(tweets$author),decreasing=TRUE)[1:15]
write.table(tweets, file=paste("Twitts - ", format(Sys.time(), "%a %b %d %H_%M_%S %Y"), ".csv"), sep = ";")
Sys.sleep(d_time)
} # end if
Run Code Online (Sandbox Code Playgroud)
我的猜测是,您的问题对应于 Twitter(或您与网络的连接)出现故障或缓慢或其他任何情况,因此得到了不好的结果。您是否尝试过设置
options(error = recover)
Run Code Online (Sandbox Code Playgroud)
那么下次你遇到错误时,就会出现一个很好的浏览器环境供你查看。