Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 从XML节点解析特定值_R_Xml_Htmlparse_Xpathsapply - Fatal编程技术网

R 从XML节点解析特定值

R 从XML节点解析特定值,r,xml,htmlparse,xpathsapply,R,Xml,Htmlparse,Xpathsapply,使用R和XML包,我使用XML htmlParse函数解析了一个(“HTMLInternalDocument”“HTMLInternalDocument”“XMLInternalDocument”“XMLAbstractDocument”)对象。我感兴趣的xml对象中的行(见下文)包含我希望返回的两个值 除了class=gsc_1usr_name(返回“Konrad Wrzecionkowski”)中的值之外,我还需要在“user=”下提取该值,在本例中是“QnVgFlYAAAAJ”。我用xpa

使用R和XML包,我使用XML htmlParse函数解析了一个(“HTMLInternalDocument”“HTMLInternalDocument”“XMLInternalDocument”“XMLAbstractDocument”)对象。我感兴趣的xml对象中的行(见下文)包含我希望返回的两个值

除了class=gsc_1usr_name(返回“Konrad Wrzecionkowski”)中的值之外,我还需要在“user=”下提取该值,在本例中是“QnVgFlYAAAAJ”。我用xpathSApply尝试了几种语法变体,它总是返回NULL。诚然,我对xml一无所知,有什么想法吗?有没有一种方法可以将其强制到不同的对象类,例如list,然后在向量上使用split?标准强制(例如,as.list、as.character)似乎不适用于此对象类

search.page <- "http://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=GVN Powell World Wildlife Fund"
x <- XML::htmlParse(search.page, encoding="UTF-8")
对xpathSApply函数使用以下语法,我返回“GVN Powell”,但也希望得到user=的值。我尝试了h3[@user='']的变体,包括类的子查询,但无法获得任何其他功能

XML::xpathSApply(x, "//h3[@class='gsc_1usr_name']", xmlValue)
我一直使用的方法是url和readLines。然后我使用strsplit提取所需的值

auth.names <- "Konrad Wrzecionkowski WWF"    
search.page <- paste("http://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=", auth.names, sep="")

x <- readLines(url(search.page))
x <- strsplit(x[[1]], split="user=")[[1]][2]
x <- strsplit(x, split="&amp;")[[1]][1]
auth.names
library(rvest)
图书馆(magrittr)
url%
html_节点(xpath=xpath)%>%
html_文本
鲍威尔
#[1] “GVN鲍威尔”

您可能可以从
标记中获取
href=
属性-xpathsaply(x,“//a”,xmlGetAttr,“href”)
有效吗?不幸的是,此语法无效。在XML中,我有10个条目要为其检索值。我修改了我的帖子,以提供更多关于xml的详细信息。删除Google违反了他们的ToS。@最近的邮件我错了,这是解决方案。它产生了一个字符向量,因此我能够使用grep来提取“user=”元素,然后使用strsplit来提取值。非常感谢。如果您将您的评论移动到答案,我会将其标记为解决方案。既然您有解决方案,您可以添加一个自我回答并接受它。看见让我知道,如果你决定张贴它;我将投票。Tks。
auth.names <- "Konrad Wrzecionkowski WWF"    
search.page <- paste("http://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=", auth.names, sep="")

x <- readLines(url(search.page))
x <- strsplit(x[[1]], split="user=")[[1]][2]
x <- strsplit(x, split="&amp;")[[1]][1]
library(rvest)
library(magrittr)

url <- "http://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=GVN Powell World Wildlife Fund"
xpath = "//*[@id=\"gsc_ccl\"]/div[1]/div[2]/h3/a/span"

gvn.powell <- url %>%
  read_html %>%
  html_nodes(xpath = xpath) %>%
  html_text

gvn.powell
#[1] "GVN Powell"