Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R、 右xpath使用xpathSApply获取文本_R_Xpath - Fatal编程技术网

R、 右xpath使用xpathSApply获取文本

R、 右xpath使用xpathSApply获取文本,r,xpath,R,Xpath,这是一个简单的xpath练习,但我无法让它工作 当我检查一个按钮的元素时(使用google chrom),它给出了这个树- 我想抓住标题,比如“杰出贡献者”或“董事会经理” ..... 到目前为止,我试过了 > xpathSApply(htmltree, "//img[@class='lia-user-rank-icon-left']", xmlGetAttr, "href") > test = "//img/@title" > a <- xpathSApply(h

这是一个简单的xpath练习,但我无法让它工作

当我检查一个按钮的元素时(使用google chrom),它给出了这个树- 我想抓住标题,比如“杰出贡献者”或“董事会经理”


.....
到目前为止,我试过了

> xpathSApply(htmltree, "//img[@class='lia-user-rank-icon-left']", xmlGetAttr, "href")

> test = "//img/@title"
> a <- xpathSApply(htmltree, test, function(x) c(xmlValue(x), xmlAttrs(x)[["href"]]))
>xpathSApply(htmltree,//img[@class='lia-user-rank-icon-left'],xmlGetAttr,“href”)
>test=“//img/@title”

>a这是一个使用类“dno”获取图像源的示例。我认为在您的情况下,您必须更改“dno”和“src”

library(RCurl)
library(XML)
text = getURL("http://stackoverflow.com/questions/23024062/r-right-xpath-to-grab-the-text-using-xpathsapply")
d = htmlParse(text)
L = xpathApply(d, "//img[@class='dno']")
sapply(L, xmlGetAttr, "src")

最后两行可以替换为
xpathApply(d,“//img[@class='dno']”,xmlGetAttr,“src”)
。但是,出于调试目的,最好将其拆分为两个命令。

在查看页面源代码时,可以右键单击所需的内容,然后选择“复制XPath”,这应该会有所帮助。实际页面是什么?href应该是title吗?是的,我认为@RandyLai是对的谢谢!是的,最好将其拆分为两个命令。:)
library(RCurl)
library(XML)
text = getURL("http://stackoverflow.com/questions/23024062/r-right-xpath-to-grab-the-text-using-xpathsapply")
d = htmlParse(text)
L = xpathApply(d, "//img[@class='dno']")
sapply(L, xmlGetAttr, "src")