R:网页抓取没有显示任何数据的表格

时间:2013-04-18 19:03:44

标签: r

我正在尝试webscrape一个选项表:

http://www.cmegroup.com/trading/metals/base/copper_quotes_settlements_options.html

我试过了:

library(XML)
options.cu<-readHTMLTable("http://www.cmegroup.com/trading/metals/base/copper_quotes_settlements_options.html",stringsAsFactors = FALSE)
options.cu

但我得到:

$ table_under_bluebar           V1 V2 V3 1交易日期类型到期

并且表格中没有数据。 我很感激你的帮助。

1 个答案:

答案 0 :(得分:1)

此表是使用javascript动态创建的。您可以检查流量以查看是否可以找到Feed。这样做你会注意到以下网址:

http://www.cmegroup.com/CmeWS/mvc/xsltTransformer.do?xlstDoc=/XSLT/da/DailySettlement.xsl

这将url作为表示产品线等的参数。例如:

http://www.cmegroup.com/CmeWS/mvc/xsltTransformer.do?xlstDoc=/XSLT/da/DailySettlement.xsl&url=/da/DailySettlement/V1/DSReport/ProductCode/HX/FOI/OOF/EXCHANGE/XCEC/Underlying/HG/ProductId/797

将得到铜选项的表,其中可能有产品ID 797.可以通过指定参数添加其他参数:

http://www.cmegroup.com/CmeWS/mvc/xsltTransformer.do?xlstDoc=/XSLT/da/DailySettlement.xsl&url=/da/DailySettlement/V1/DSReport/ProductCode/HX/FOI/OOF/EXCHANGE/XCEC/Underlying/HG/ProductId/797?tradeDate=04/17/2013&monthYear=null&optionTypeName=Daily&optionType=AME

您应该可以使用此Feed来获取相关数据:

xmlD <- "http://www.cmegroup.com/CmeWS/mvc/xsltTransformer.do?xlstDoc=/XSLT/da/DailySettlement.xsl&url=/da/DailySettlement/V1/DSReport/ProductCode/HX/FOI/OOF/EXCHANGE/XCEC/Underlying/HG/ProductId/797"

library(XML)
options.cu <- readHTMLTable(xmlD,stringsAsFactors = FALSE)
str(options.cu)
#List of 1
# $ DailySettlementTable:'data.frame':  337 obs. of  10 variables:
#  ..$ V1 : chr [1:337] "Daily Settlements for Copper American Options (FINAL)Trade Date: 04/18/2013" "Strike" "025" "050" ...
#  ..$ V2 : chr [1:337] NA "Type" "Call" "Call" ...
#  ..$ V3 : chr [1:337] NA "Open" "-" "-" ...
#  ..$ V4 : chr [1:337] NA "High" "-" "-" ...
#  ..$ V5 : chr [1:337] NA "Low" "-" "-" ...
#  ..$ V6 : chr [1:337] NA "Last" "-" "-" ...
#  ..$ V7 : chr [1:337] NA "Change" "+.0170" "+.0170" ...
#  ..$ V8 : chr [1:337] NA "Settle" "2.9545" "2.7045" ...
#  ..$ V9 : chr [1:337] NA "EstimatedVolume" "-" "-" ...
#  ..$ V10: chr [1:337] NA "Prior DayOpen Interest" "-" "-" ...