Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从多语言网站下载.file()_R - Fatal编程技术网

R 从多语言网站下载.file()

R 从多语言网站下载.file(),r,R,当我导航到此页面()并单击“导航/索引历史记录”链接(在“市场信息”下)下载CSV文件时,该文件的内容是英文的。但是,如果我尝试通过R使用以下命令执行同样的操作,则内容是中文的: download.file("http://hk.ishares.com/product_info/fund/excel_histoverview.htm?ticker=2836", destfile="2836.csv") a <- read.csv("2836.csv", skip = 5) > hea

当我导航到此页面()并单击“导航/索引历史记录”链接(在“市场信息”下)下载CSV文件时,该文件的内容是英文的。但是,如果我尝试通过R使用以下命令执行同样的操作,则内容是中文的:

download.file("http://hk.ishares.com/product_info/fund/excel_histoverview.htm?ticker=2836", destfile="2836.csv")
a <- read.csv("2836.csv", skip = 5)
> head(a)
            日期 指數收市水平..HKD. 單位資產淨值 總派息.每股...HKD.
1 2013年03月18日          3666.9390      15.7774                  0
2 2013年03月15日          3701.3143      15.9145                  0
3 2013年03月14日          3709.7446      15.9484                  0
4 2013年03月13日          3668.3178      15.8762                  0
5 2013年03月12日          3707.0364      15.9726                  0
6 2013年03月11日          3716.4011      16.0521                  0
download.file(“http://hk.ishares.com/product_info/fund/excel_histoverview.htm?ticker=2836“,destfile=“2836.csv”)
a头(a)
日期 指數收市水平..港币。單位資產淨值 總派息.每股...港币。
1 2013年03月18日          3666.9390      15.7774                  0
2 2013年03月15日          3701.3143      15.9145                  0
3 2013年03月14日          3709.7446      15.9484                  0
4 2013年03月13日          3668.3178      15.8762                  0
5 2013年03月12日          3707.0364      15.9726                  0
6 2013年03月11日          3716.4011      16.0521                  0
大概是因为该网站在我的浏览器中识别出一个cookie,并向我提供文件的英文版本,但不是在我浏览R

有没有办法绕过这个问题?将CSV文件转换为XTS会给我带来一些困难,因为我不知道如何将中文日期转换为日期对象


谢谢。

如果已安装httr库,请尝试以下操作:

library(httr)

# Get cookies from English page
en_page <- GET("http://hk.ishares.com/product_info/fund/overview/SEHK/2836.htm?ls=true&l=en")

# Get the data
data <- GET("http://hk.ishares.com/product_info/fund/excel_histoverview.htm?ticker=2836",
        set_cookies(en_page$cookies[[1]]))

# Load into a data.frame
a <- read.csv(textConnection(content(data)), skip = 5)

head(a)
库(httr)
#从英文页面获取cookies

您是否尝试过类似于
gsub(“[^[:digit:][]”,“-”,a[,1])
的快速修复方法?可能该网站正在使用基于客户端发送的
Accept Language
标题的内容协商?@sebastian-c,几乎奏效。我得到了一个额外的连字符
“2009-02-10-”
@sebastian-c这就完成了技巧
sapply(a[,1],函数(x)substr(x,1,nchar(x)-1))
这很漂亮。谢谢