【发布时间】:2017-09-19 21:06:04
【问题描述】:
我正在尝试通过 XHR 请求抓取动态网站 Morningstar.com。
我正在抓取的确切网站是:http://performance.morningstar.com/funds/etf/total-returns.action?t=SPY®ion=USA&culture=en_US
我要抓取的是季度绩效数字(1 个月)。截至今天,结果应该是 0.64。
try(res <- GET(url = "http://performance.morningstar.com/fund/performance-return.action",
query = list(
t="SPY",
region="usa",
culture="en-US"
)
))
tryCatch(x <- content(res) %>%
html_nodes(xpath = '//*[@id="tab-quar-end-content"]/table/tbody/tr[1]/td[1]') %>%
html_text() %>%
trimws() %>%
as.numeric()
, error = function(e) x <-NA)
但是,结果是 numeric(0)
知道我做错了什么吗?
苏迪
更新:
我能够使用以下代码获取 html 数据:
try(res <- GET(url = "http://performance.morningstar.com/fund/performance-return.action",
query = list(
t = "SPY",
region = "usa",
culture = "en-US",
ops = "clear",
s = "0P0000J533",
ndec = "2",
ep = "true",
align = "q",
annlz = "true",
comparisonRemove = "false"
)
))
但我仍然无法使用 CSS 选择器或带有 rvest 的 xpath 来指向数据。
你们用什么来查找这些数据点? SelectorGadget 仍然是首选吗?
干杯,亚伦
【问题讨论】:
-
您应该考虑更新您的问题而不是发布答案。
-
好点!新手。
标签: r web-scraping rvest