使用右标记(类、div、span、表等)在R中使用rvest

使用右标记(类、div、span、表等)在R中使用rvest,r,rvest,R,Rvest,我已经开始使用rvest包,并且遇到了一些一致的问题,即如何准确地引用HTML代码 例如,下面的代码返回一个空字符(最终需要0.74)。基本上,我唯一能返回的就是使用“div”作为节点,它只返回所有文本。“tr.total-return”、“total return”、“div.sal-trailing-return\uuuu middle”也都返回null a=read_html(https://www.morningstar.com/funds/xnas/hcyix/performance)

我已经开始使用
rvest
包,并且遇到了一些一致的问题,即如何准确地引用HTML代码

例如,下面的代码返回一个空字符(最终需要0.74)。基本上,我唯一能返回的就是使用“div”作为节点,它只返回所有文本。“tr.total-return”、“total return”、“div.sal-trailing-return\uuuu middle”也都返回null

a=read_html(https://www.morningstar.com/funds/xnas/hcyix/performance)
b=html_nodes(a, "td")

页面动态加载。因此,您需要使用
RSelenium
,而不仅仅是
rvest

此代码用于获取
0.74
的数据点

library(rvest)
library(tidyverse)
library(RSelenium)

url<- "https://www.morningstar.com/funds/xnas/hcyix/performance"

# RSelenium with Firefox
rD <- RSelenium::rsDriver(browser="firefox", port=4546L, verbose=F)
remDr <- rD[["client"]]
remDr$navigate(url)
Sys.sleep(4)

# get the page source
web <- remDr$getPageSource()
web <- xml2::read_html(web[[1]])

b <- html_node(web, ".total-return > td:nth-child(1)") %>%
  html_text() %>%
  trimws()

# close RSelenium
remDr$close()
gc()
rD$server$stop()
system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
库(rvest)
图书馆(tidyverse)
图书馆(资源库)

页面动态加载的url。因此,您需要使用
RSelenium
,而不仅仅是
rvest

此代码用于获取
0.74
的数据点

library(rvest)
library(tidyverse)
library(RSelenium)

url<- "https://www.morningstar.com/funds/xnas/hcyix/performance"

# RSelenium with Firefox
rD <- RSelenium::rsDriver(browser="firefox", port=4546L, verbose=F)
remDr <- rD[["client"]]
remDr$navigate(url)
Sys.sleep(4)

# get the page source
web <- remDr$getPageSource()
web <- xml2::read_html(web[[1]])

b <- html_node(web, ".total-return > td:nth-child(1)") %>%
  html_text() %>%
  trimws()

# close RSelenium
remDr$close()
gc()
rD$server$stop()
system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
库(rvest)
图书馆(tidyverse)
图书馆(资源库)

URL当我转到那个URL时,我看不到任何数据。您必须登录才能访问该网站吗?复制到overflow时胖翻了一下,现在应该是正确的文档源中没有
标记(这与您在“元素”选项卡中看到的不同--请查看“源”选项卡以了解rvest可以看到的内容)。这些是通过javascript加载后创建的。rvest无法为您运行javascript。如果你想与需要javascript的网页交互,你需要像
RSelenium
这样的东西。数据来自这个端点
https://api-global.morningstar.com/sal-service/v1/fund/trailingReturn/v2/F00000Q1AI/data?locale=en&duration=daily¤cy=&languageId=en&locale=en&clientId=MDC&benchmarkId=category&component=sal-组件mip trailing return&version=3.31.0
-您可能需要检查T&C。然后返回的json中的路径是object►总返回导航►0,但R中的索引将为1。当我转到该URL时,我看不到任何数据。您必须登录才能访问该网站吗?复制到overflow时胖翻了一下,现在应该是正确的文档源中没有
标记(这与您在“元素”选项卡中看到的不同--请查看“源”选项卡以了解rvest可以看到的内容)。这些是通过javascript加载后创建的。rvest无法为您运行javascript。如果你想与需要javascript的网页交互,你需要像
RSelenium
这样的东西。数据来自这个端点
https://api-global.morningstar.com/sal-service/v1/fund/trailingReturn/v2/F00000Q1AI/data?locale=en&duration=daily¤cy=&languageId=en&locale=en&clientId=MDC&benchmarkId=category&component=sal-组件mip trailing return&version=3.31.0
-您可能需要检查T&C。然后返回的json中的路径是object►总返回导航►0,但R中的索引将为1。谢谢,看起来我必须阅读该文档!谢谢,看来我得仔细阅读一下那个文档了!