我正在尝试使用R从网页下载并提取一个.csv文件。这个问题是“使用R下载压缩的数据文件,提取和导入数据”的重复。但我无法使解决方案起作用,可能是因为我使用的网址不对。我正在尝试从http://data.worldbank.org/country/united-kingdom(在“下载数据”下拉菜单下)下载.csv文件。使用上面链接中@Dirk的解决方案,我尝试了:
我通过查看页面源代码获取了扩展链接,我认为这可能是导致问题的原因,但如果我将其粘贴到地址栏中,它仍然可以正常工作。
该文件下载大小正确。
temp <- tempfile()
download.file("http://api.worldbank.org/v2/en/country/gbr?downloadformat=csv",temp)
con <- unz(temp, "gbr_Country_en_csv_v2.csv")
dat <- read.table(con, header=T, skip=2)
unlink(temp)
我通过查看页面源代码获取了扩展链接,我认为这可能是导致问题的原因,但如果我将其粘贴到地址栏中,它仍然可以正常工作。
该文件下载大小正确。
download.file("http://api.worldbank.org/v2/en/country/gbr?downloadformat=csv",temp)
# trying URL 'http://api.worldbank.org/v2/en/country/gbr?downloadformat=csv'
# Content type 'application/zip' length 332358 bytes (324 Kb)
# opened URL
# downloaded 324 Kb
# also tried unzip but get this warning
con <- unzip(temp, "gbr_Country_en_csv_v2.csv")
# Warning message:
# In unzip(temp, "gbr_Country_en_csv_v2.csv") :
# requested file not found in the zip file
但这些是我手动下载时的文件名。
我需要一些帮助,告诉我出了哪些问题,谢谢。
我正在使用Windows 8操作系统,R版本为3.1.0。