Created
December 6, 2012 08:59
-
-
Save yanping/4223011 to your computer and use it in GitHub Desktop.
获取大商所期货数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 从大连商品交易所网站抓取价格数据 数据源 http://www.dce.com.cn/PublicWeb/MainServlet?action=Pu00011_search | |
# 接口举例 http://www.dce.com.cn/PublicWeb/MainServlet?Pu00011_Input.trade_date=20121206&Pu00011_Input.trade_type=0&Pu00011_Input.variety=a&action=Pu00012_download | |
# Pu00011_Input.trade_date : 查询的日期 | |
# Pu00011_Input.trade_type : 行情类别 0为期货,1为期权(未开通) | |
# Pu00011_Input.variety : 期货品种代码,各代码表示的期货品种: | |
# a : 豆一 | |
# b : 豆二 | |
# c : 玉米 | |
# j :焦炭 | |
# l : 聚乙烯 | |
# m : 豆粕 | |
# p : 棕榈油 | |
# v : 聚氯乙烯 | |
# y : 豆油 | |
# s : 大豆 | |
dceDatabase <- NULL | |
for(date in c("20121205","20121206")){ | |
for(type in c("a","b","c","j","l","m","p","v","y") ) { | |
str1 <- "http://www.dce.com.cn/PublicWeb/MainServlet?Pu00011_Input.trade_date=" | |
str2 <- "&Pu00011_Input.trade_type=0&Pu00011_Input.variety=" | |
str3 <- "&action=Pu00012_download" | |
url <- paste(str1,date,str2,type,str3,sep="") | |
fileName <- paste("dce.",type,date,".txt",sep="") | |
lines <- readLines(url) | |
if(any(lines=="<html>")|length(lines)==5){ | |
next | |
}else{ | |
content <- lines[4:(length(lines)-2)] | |
writeLines(content,fileName) | |
data <- read.table(fileName,header=T,na.strings="-") | |
data$date <- date | |
if(is.null(database)){ | |
dceDatabase <- data | |
}else{ | |
dceDatabase <- rbind(dceDatabase,data) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment