从网站下载zip中的csv错误,带入R [英] Error downloading a csv in zip from website with get in R
本文介绍了从网站下载zip中的csv错误,带入R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用download.file()从nse-india.com读取R中的数据,如下所示。
I would like to read data from nse-india.com to R using download.file() as shown below.
url = 'http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip'
temp = tempfile()
download.file(url, destfile = temp,method = 'wget')
它会抛出以下错误:
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\PROGRA~2\GnuWin32/etc/wgetrc
--2014-09-28 21:19:26-- http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip
Resolving www.nseindia.com... 202.83.22.200, 202.83.22.203
Connecting to www.nseindia.com|202.83.22.200|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2014-09-28 21:19:26 ERROR 403: Forbidden.
Warning messages:
1: running command 'wget "http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip" -O "C:\Users\ITITHI~1\AppData\Local\Temp\Rtmp2fjADx\file1fb02375882"' had status 1
2: In download.file(url, destfile = temp, method = "wget") :
download had nonzero exit status
请让我知道,
编辑:或者从R中下载文件的任何其他方法也是很棒的。
Or any other method to download the file from within R would also be great.
推荐答案
您需要设置类似浏览器的用户代理字符串,以便网站认为您是一个浏览器,而不是自动化的刮板/下载机器人:
You need to set a browser-like user agent string so the site thinks you're a browser vs an automated scraper/downloader robot:
library(httr) # >=v0.5
GET("http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip",
user_agent("Mozilla/5.0"), write_disk("cm24SEP2014bhav.csv.zip"))
## Response [http://www.nseindia.com/content/historical/EQUITIES/2014/SEP/cm24SEP2014bhav.csv.zip]
## Date: 2014-09-28 23:53
## Status: 200
## Content-type: application/zip
## Size: 58.2 kB
## <ON DISK> cm24SEP2014bhav.csv.zip
这篇关于从网站下载zip中的csv错误,带入R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文