我终于能够计算出我的抓取代码了.它似乎工作正常,然后突然再次运行它时,我收到以下错误消息:
Error in url[i] = paste("http://en.wikipedia.org/wiki/", gsub(" ", "_", :
object of type 'closure' is not subsettable
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我在代码中没有改变任何内容.
请指教.
library(XML)
library(plyr)
names <- c("George Clooney", "Kevin Costner", "George Bush", "Amar Shanghavi")
for(i in 1:length(names)) {
url[i] = paste('http://en.wikipedia.org/wiki/', gsub(" ","_", names[i]) , sep="")
# some parsing code
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下语法从George Clooney的维基百科页面获取职业信息.最终我希望有一个循环来获取各种人格职业的数据.
但是,运行以下代码时出现以下问题:
Run Code Online (Sandbox Code Playgroud)Error in if (symbol != "role") symbol = NULL : argument is of length zero
我不确定为什么会继续这样.
library(XML)
library(plyr)
url = 'http://en.wikipedia.org/wiki/George_Clooney'
# don't forget to parse the HTML, doh!
doc = htmlParse(url)
# get every link in a table cell:
links = getNodeSet(doc, '//table/tr/td')
# make a data.frame for each node with non-blank text, link, and 'title' attribute:
df = ldply(links, function(x) {
text = xmlValue(x)
if (text=='') text=NULL
symbol = xmlGetAttr(x, 'class')
if (symbol!='role') symbol=NULL
if(!is.null(text) …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行以下操作:
try(htmlParse(ip[1], T),
在哪里我定义为:
ip[1] = paste('http://en.wikipedia.org/wiki/George_Clooney')
Run Code Online (Sandbox Code Playgroud)
我想检查htmlParse是否有效.对于我的列表中的许多名称,将没有维基百科站点,因此如果维基页面不存在,我需要能够检查并用NA替换ip [1].
有人可以告诉我如何做到这一点.我尝试使用命令geterrmessage(),但是我不知道每次更改名人的名字时如何刷新.
目前我有以下内容:
if(!isTRUE(as.logical(grep(ip[1],err)))) {
ip[1] = NA
}
else {
Run Code Online (Sandbox Code Playgroud)
这肯定是错误的,因为它没有运行我想要的逻辑语句.
谢谢
阿马尔