【问题标题】:Issue with piping and rvest管道和 rvest 问题
【发布时间】:2018-06-12 16:12:24
【问题描述】:

我正在尝试使用 rvest 从ASX 中获取期权价格,我希望有人帮助我管理我的代码。我想管道并最终得到一个数据框

上面链接的页面有两个表格,第一个表格包含股价信息,第二个表格包含所有选项。当我运行以下代码时,我得到第二个表的数据框:

html <- read_html("http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=ANZ&expiryDate=&optionType=B")
nodes <- html_nodes(html, "table.options")
df <- html_table(nodes)[[2]]

但是,当我尝试使用以下内容管道相同的代码时:

html <- read_html("http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=ANZ&expiryDate=&optionType=B")
html %>% 
 html_nodes("table.options") %>% 
 html_table()[[2]]

我收到一个错误,读取“UseMethod("html_table") 中的错误: 没有适用于 'html_table' 的方法应用于“NULL”类的对象'

谁能告诉我我做错了什么?

【问题讨论】:

标签: r web-scraping rvest piping


【解决方案1】:

如果你想直接索引,你必须像这样在单独的管道中索引

read_html("http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=ANZ&expiryDate=&optionType=B") %>%
html_nodes("table.options") %>%
html_table() %>%
.[[2]]

其中. 充当前一个管道的输出。

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2020-11-05
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    相关资源
    最近更新 更多