使用以下 documentation我一直在尝试从 marketwatch.com 上抓取一系列表格
这是下面的代码所代表的:
链接和 xpath 已经包含在代码中:
url <- "http://www.marketwatch.com/investing/stock/IRS/profile"
valuation <- url %>%
html() %>%
html_nodes(xpath='//*[@id="maincontent"]/div[2]/div[1]') %>%
html_table()
valuation <- valuation[[1]]
我收到以下错误:
Warning message:
'html' is deprecated.
Use 'read_html' instead.
See help("Deprecated")
提前致谢。
最佳答案
该网站不使用 html 表格,所以 html_table()
找不到任何东西。它实际上使用 div
类(class) column
和 data lastcolumn
.
所以你可以做类似的事情
url <- "http://www.marketwatch.com/investing/stock/IRS/profile"
valuation_col <- url %>%
read_html() %>%
html_nodes(xpath='//*[@class="column"]')
valuation_data <- url %>%
read_html() %>%
html_nodes(xpath='//*[@class="data lastcolumn"]')
甚至url %>%
read_html() %>%
html_nodes(xpath='//*[@class="section"]')
让你一路走好。另请阅读他们的 terms of use - 特别是 3.4。
关于r - 如何用 rvest 和 xpath 刮一张 table ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35707534/