【问题标题】:R: json file from API on computer with company networkR:来自具有公司网络的计算机上 API 的 json 文件
【发布时间】: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

标签: json r api firewall


【解决方案1】:

这是另一个潜在的替代解决方案:

library(RSelenium)
library(XML)
shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox")
remDr$open()
remDr$navigate("http://api.gios.gov.pl/pjp-api/rest/station/findAll")
Sys.sleep(5)

page_Content <- remDr$getPageSource()[[1]]
readHTMLTable(page_Content)

这是另一个潜在的替代解决方案:

library(pdftools)
library(pagedown)
library(stringr)
library(jsonlite)
chrome_print("http://api.gios.gov.pl/pjp-api/rest/station/findAll",
             "C:\\...\\json_pdf.pdf")

text <- pdf_text("C:\\...\\json_pdf.pdf")
text <- paste0(text, collapse = "")
text <- str_remove_all(text, pattern = "\\r\\n")
result <- jsonlite::fromJSON(text)

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2018-01-02
    相关资源
    最近更新 更多