R html_nodes() 函数给出错误意外字符'$'
R html_nodes() function giving error Unexpected character '$'
我正在尝试从 Yahoo Finance 中提取财务数据。当我 运行 时,出现错误
"Error in tokenize(css) : Unexpected character '$' found at position 19"
urlYCashflow <- "https://au.finance.yahoo.com/quote/MSFT/cash-flow?p=MSFT"
webpageYCashflow <- read_html(urlYCashflow)
node1 <- webpageYCashflow %>%
html_nodes('D(tbr).fi-row.Bgc($hoverBgColor):h') %>%
html_text()
有什么方法可以通过在 XML 文档或任何其他建议中替换它来避免 $ 吗?我也试过 xpath 标签,但每次结果都是字符(0)。
node1 <- webpageYCashflow %>%
html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/div[1]/div/div[2]/div[7]/div[2]/div[3]/div[1]/div[2]/span') %>%
html_text()
您具体追求哪些价值?您可以使用以下内容: 获取所有值。
目前您的 css selector 在语法上不正确,尤其是使用未转义的 $ 和 :h,它们分别以运算符和隐含的伪 selector 结尾。编译时,这就是它们的解释方式。您还缺少前导 class selector。您可以简单地将多值 class 替换为单个 class 名称 .fi-row
以获取行。
要匹配您的 xpath,您只需 select 最后一行,然后是第二列:
library(rvest)
library(magrittr)
page <- read_html('https://au.finance.yahoo.com/quote/MSFT/cash-flow?p=MSFT')
free_cash_flow <- tail(page%>%html_nodes('.fi-row'),1)%>%html_nodes('span')%>%`[[`(2)%>%html_text()
我正在尝试从 Yahoo Finance 中提取财务数据。当我 运行 时,出现错误
"Error in tokenize(css) : Unexpected character '$' found at position 19"
urlYCashflow <- "https://au.finance.yahoo.com/quote/MSFT/cash-flow?p=MSFT"
webpageYCashflow <- read_html(urlYCashflow)
node1 <- webpageYCashflow %>%
html_nodes('D(tbr).fi-row.Bgc($hoverBgColor):h') %>%
html_text()
有什么方法可以通过在 XML 文档或任何其他建议中替换它来避免 $ 吗?我也试过 xpath 标签,但每次结果都是字符(0)。
node1 <- webpageYCashflow %>%
html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/div[1]/div/div[2]/div[7]/div[2]/div[3]/div[1]/div[2]/span') %>%
html_text()
您具体追求哪些价值?您可以使用以下内容:
目前您的 css selector 在语法上不正确,尤其是使用未转义的 $ 和 :h,它们分别以运算符和隐含的伪 selector 结尾。编译时,这就是它们的解释方式。您还缺少前导 class selector。您可以简单地将多值 class 替换为单个 class 名称 .fi-row
以获取行。
要匹配您的 xpath,您只需 select 最后一行,然后是第二列:
library(rvest)
library(magrittr)
page <- read_html('https://au.finance.yahoo.com/quote/MSFT/cash-flow?p=MSFT')
free_cash_flow <- tail(page%>%html_nodes('.fi-row'),1)%>%html_nodes('span')%>%`[[`(2)%>%html_text()