【发布时间】:2018-08-24 00:35:27
【问题描述】:
我正在尝试制作一个脚本,该脚本将从波兰的 API 官方空气质量控制 (http://powietrze.gios.gov.pl/pjp/content/api) 下载文件。服务器上的位置是:
URL <- "http://api.gios.gov.pl/pjp-api/rest/station/findAll"
问题是我在远程服务器和公司网络上工作,这可能是由防火墙或代理引起的。我之前在网页抓取方面遇到过类似的问题,但 rvest Error in open.connection(x, "rb") : Timeout was reached 的解决方案有所帮助。不幸的是,这一次并非如此。我尝试下载文件:
URL <- "http://api.gios.gov.pl/pjp-api/rest/station/findAll"
File_name <- "tmp.csv"
download.file(URL, destfile = File_name, quiet=TRUE)
但后来文件不可读(由于 readTableHeader 不完整)。
当我尝试以https://www.tutorialspoint.com/r/r_json_files.htm 格式下载.json 格式的文件并使用fromJSON 阅读时,我得到Error in fromJSON(file = File_name) : argument "txt" is missing, with no default
我也尝试按照https://cran.r-project.org/web/packages/jsonlite/vignettes/json-apis.html 中的建议使用fromJSON(URL),但出现错误:Error in open.connection(con, "rb") : Timeout was reached。我改了options(timeout= 4000000),但是id没用。
我还尝试了GET(URL) 和https://www.r-bloggers.com/accessing-apis-from-r-and-a-little-r-programming/ 一样,还使用了Can't use jsonlite in R to read json format file 中的progress() 和verbose() 参数
编辑
正如@Junhee Shin 所建议的,我尝试了以下方法:
-
wget工作了超过 5 分钟但没有产生任何东西 -
internal导致错误Error in download.file(URL, destfile = File_name, method = "internal") : cannot open URL 'http://api.gios.gov.pl/pjp-api/rest/station/findAll' In addition: Warning message: In download.file(URL, destfile = File_name, method = "internal") : unable to connect to 'api.gios.gov.pl' on port 80. -
wininet有效(有错误Content type 'application/json;charset=UTF-8' length unknown,但fromJSON有错误argument "txt" is missing, with no default -
libcurl两次使我的 R 会话崩溃 -
curl导致错误:Warning messages: 1: running command 'curl "http://api.gios.gov.pl/pjp-api/rest/station/findAll" -o "D:\magisterka\Wroclaw Open Data\tmp.json"' had status 127 2: In download.file(URL, destfile = File_name, method = "curl") : download had nonzero exit status -
auto有效(有错误Content type 'application/json;charset=UTF-8' length unknown,但fromJSON有错误argument "txt" is missing, with no default
编辑 2
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
【问题讨论】:
-
试试这个。 download.file(URL, File_name, 'wget') 下载加载方法。 “自动”、“内部”、“libcurl”、“wget”、“curl”
-
@JunheeShin 我已经尝试过了(并用 EDIT 编写),但没有成功
-
我认为这是一个防火墙问题,因为我使用 fromJSON(URL) 方法没有问题。我找到了这个stackoverflow.com/questions/47528321/…,希望对您有所帮助。
-
@TTR 我试过了,但是出现了错误
Error in curl::curl_fetch_memory(url, handle = handle) : Timeout was reached