2017-09-13 72 views
0

正在使用R和rvest从www.nseindia.com网站抓取数据。我第一次能够下载数据,但之后出现以下错误消息...在R网站上抓取数据网页

使用方法错误(“xml_find_all”): 没有适用于'xml_find_all'的方法应用于类“字符的对象“

想获得股指期货的第一行

我的代码如下

library("rvest") 

    website_nifty_future_live<- read_html("https://www.nseindia.com/live_market/dynaContent/live_watch/fomwatchsymbol.jsp?key=NIFTY&Fut_Opt=Futures") 

    nifty_spot<- website_nifty_future_live %>% 
     + html_nodes(".alt:nth-child(2) td:nth-child(13)") %>% 
     + html_text() 
    nifty_spot<-as.numeric(gsub(",","",nifty_spot)) 
+0

我已经测试了MacOS和Debian上的代码。工作正常,评估后没有错误。 rvest版本0.3.2,R版本R版本3.3.3。 – Gonzo

+0

正在使用Windows时,您重新运行代码时会发生问题。感谢您的反馈意见。欣赏! – Himadri

+0

代码中的“+”会导致该错误。尝试删除“+”号后 – SBista

回答

0

的错误很可能是由于‘在你的代码的开头+’的招牌 - 我没有得到th删除它们时出错。

我建议使用下面的代码阅读完整的表作为data.frame:

library("rvest") 

url_nifty <- "https://www.nseindia.com/live_market/dynaContent/live_watch/fomwatchsymbol.jsp?key=NIFTY&Fut_Opt=Futures" 
website_nifty_future_live<- read_html(url_nifty) 

nifty_spot<- website_nifty_future_live %>% 
    html_nodes("#tab26Content > table:nth-child(1)") %>% 
    html_table(header = NA, trim = TRUE, fill = FALSE, dec = ".") %>% 
    as.data.frame() 

它是那么当然很容易得到的第一行含。标题,例如与

nifty_spot[1, ] 
    Instrument Underlying Expiry.Date Option.Type Strike.Price Open.Price High.Price Low.Price Prev..Close Last.Price Volume Turnover.lacs. 
1 Index Futures  NIFTY 28SEP2017   -   - 10,105.00 10,144.70 10,078.00 10,107.90 10,096.90 94,799 7,18,943.53 
    Underlying..Value 
1   10079.3 

希望它有帮助!