2

我试图从以下网页中抓取 R 中的到期日期数据:https ://www.theice.com/productguide/ProductSpec.shtml?specId=251#expiry 。此页面包含多个选项卡,到期日期只是其中之一。我使用的代码是

library(RCurl)
Canola <- 'https://www.theice.com/productguide/ProductSpec.shtml?specId=251#expiry'
WS <- getURL(Canola,ssl.verifypeer=FALSE)
library(XML)
ParsedData <- htmlParse(WS)
CanolaExpDate <- readHTMLTable(ParsedData)
names(CanolaExpDate)

然而,最终输出是第一个标签产品规范的交易时间。

我是网络抓取的新手,不了解 html。请告知。

4

1 回答 1

1

我在该页面的源代码中搜索了“过期”,并查看了 URL 是如何形成的。添加&expiryDates而不是#expiry导致更易于解析的表。

library(RCurl)
library(XML)
Canola <- "https://www.theice.com/productguide/ProductSpec.shtml?specId=251&expiryDates"
WS <- getURL(Canola)
x <- readHTMLTable(WS, stringsAsFactors=FALSE)
as.data.frame(lapply(x[[1]], as.Date, format="%a %b %d %X"))

#   Contract.Symbol        FTD        LTD        FND        LND        FDD        LDD Options.FTD Options.LTD
#1       2013-07-01 2013-05-16 2013-07-12 2013-06-28 2013-07-15 2013-07-02 2013-07-16        <NA>  2013-06-21
#2       2013-08-01 2013-03-25 2013-07-26 2013-07-31 2013-08-15 2013-08-01 2013-08-16        <NA>  2013-07-26
#3       2013-09-01 2013-08-27 2013-08-23 2013-08-30 2013-09-16 2013-09-03 2013-09-17        <NA>  2013-08-23
#4       2013-10-01 2013-05-27 2013-09-20 2013-09-30 2013-10-15 2013-10-01 2013-10-16        <NA>  2013-09-20
#5       2013-11-01 2013-07-15 2013-11-14 2013-10-31 2013-11-15 2013-11-01 2013-11-18        <NA>  2013-10-25
#6       2013-01-01 2013-11-15 2013-01-14 2013-12-31 2013-01-15 2013-01-02 2013-01-16        <NA>  2013-12-20
#7       2013-03-01 2013-01-17 2013-03-14 2013-02-28 2013-03-17 2013-03-03 2013-03-18        <NA>  2013-02-21
#8       2013-05-01 2013-03-15 2013-05-14 2013-04-30 2013-05-15 2013-05-01 2013-05-16        <NA>  2013-04-25
#9       2013-07-01 2013-05-15 2013-07-14 2013-06-30 2013-07-15 2013-07-02 2013-07-16        <NA>  2013-06-20
#10      2013-11-01 2013-07-16 2013-11-14 2013-10-31 2013-11-17 2013-11-03 2013-11-18        <NA>  2013-10-24
#11      2013-01-01 2013-11-15 2013-01-14 2013-12-31 2013-01-15 2013-01-02 2013-01-16        <NA>  2013-12-19
#12      2013-03-01 2013-01-15 2013-03-13 2013-02-27 2013-03-16 2013-03-02 2013-03-17        <NA>  2013-02-20
#13      2013-05-01 2013-03-15 2013-05-14 2013-04-30 2013-05-15 2013-05-01 2013-05-19        <NA>  2013-04-24
#14      2013-07-01 2013-05-15 2013-07-14 2013-06-30 2013-07-15 2013-07-02 2013-07-16        <NA>  2013-06-26

编辑:更多关于我如何找到上面使用的 URL。我实际上没有使用任何开发人员工具。我只是右键单击并选择“查看源代码”并搜索“到期”。有一个app.urls部分有这样的东西

'expiry':'/productguide/ProductSpec.shtml;jsessionid=C59BE223F113CFDD340BF23CC07EEFFC?expiryDates=&specId=251'

所以,我尝试省略 jsessionid 部分,然后我去了

https://theice.com/productguide/ProductSpec.shtml?expiryDates=&specId=251

它看起来很有趣。我只将它重新排序为https://www.theice.com/productguide/ProductSpec.shtml?specId=251&expiryDates

因为我认为这样的 URL 看起来更好。

于 2013-07-08T20:53:12.253 回答