gpt4 book ai didi

xml - 在 R XML Xpath 中,@href 返回文本 "href"

转载 作者:行者123 更新时间:2023-12-03 17:03:12 25 4
gpt4 key购买 nike

我正在尝试使用 Xpath 代码获取 href 的内容,如这些 two 中所述。 posts 。不幸的是,除了 URL 之外,代码还返回实际文本“href”和几个空格。我怎样才能避免这种情况?

library(XML)

html <- readLines("http://www.msu.edu")
html.parse <- htmlParse(html)
Node <- getNodeSet(html.parse, "//div[@id='MSU-top-utilities']//a/@href")
Node[[1]]

# > Node[[1]]
# href
# "students/index.html"
# attr(,"class")
# [1] "XMLAttributeValue"

最佳答案

这只是一个命名的字符向量。你可以这样做:

as.character(Node[[1]])

这会给你

## [1] "students/index.html"

或者,这里有 xml2 包中更好的习惯用法:

library(xml2)

doc <- read_html("http://www.msu.edu")
nodes <- xml_find_all(doc, "//div[@id='MSU-top-utilities']//a")
xml_attr(nodes, "href")

## [1] "students/index.html" "faculty-staff/index.html" "alumni/index.html"
## [4] "businesses/index.html" "visitors/index.html"

关于xml - 在 R XML Xpath 中,@href 返回文本 "href",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com