管道和rvest的问题

时间:2018-01-03 00:34:18

标签: r web-scraping rvest piping

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

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

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”'

的对象

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

1 个答案:

答案 0 :(得分:0)

如果要直接索引,则必须在单独的管道中进行索引,如此

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]]

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

相关问题