gpt4 book ai didi

r - 如何用 rvest 和 xpath 刮一张 table ?

转载 作者:行者123 更新时间:2023-12-03 15:24:28 27 4
gpt4 key购买 nike

使用以下 documentation我一直在尝试从 marketwatch.com 上抓取一系列表格

这是下面的代码所代表的:

enter image description here

链接和 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) columndata 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/

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