如何从加拿大水调查网站下载并读取.csv文件(每日流量)?

问题描述 投票:0回答:1

我试图让 R 自动从加拿大水调查网站下载并读取 .csv 文件(每日流量)到本地目录。要手动执行此操作,我必须单击“下载?”按钮在此链接。然后,我需要单击“放电(每日平均值)”下的“逗号分隔值”。

从上面的链接可以清楚地看出,我可以修改“stn”(站号)、“startDate”和“endDate”来获取不同站和时段的数据。

但是,当我点击“下载?”时按钮后,链接将更改为 https://wateroffice.ec.gc.ca/download/index_e.html?results_type=real_time,其中不包含任何车站信息。我想知道如何下载和阅读不同站点和时段的“放电(日平均值)”.csv 文件?

library(httr)
library(readr)

base_url <- "https://wateroffice.ec.gc.ca/report/real_time_e.html?stn=07BJ001&mode=Table&startDate=2024-06-20&endDate=2024-07-23&prm1=46&y1Max=&y1Min=&prm2=47&y2Max=&y2Min=#wb-auto-4"
response <- GET(base_url)
r csv download
1个回答
0
投票

您在第一步中选择的电台和日期不会像您正确观察到的那样通过 URL 传递,而是在服务器上“记住”。当您第一次选择电台和日期时,您会获得一个会话 cookie,并且通过此 cookie,服务器可以检索您的选择(我绝不是 WebDev,所以请原谅我缺乏正确的技术术语)。

也就是说,我们所要做的就是在脚本中模拟这个过程:

  1. 我们使用站号和日期发出第一个请求,但我们会小心存储 cookie。
  2. 然后,我们调用下载链接并确保传递这个 cookie,以便服务器识别我们之前的请求。 1,最终我们得到一个zip回来,我们可以存储并进一步处理
library(httr2)
library(glue)
library(readr)

from <- as.Date("2024-06-20")
to <- as.Date("2024-07-23")
stn <- "07BJ001"

select_url <- glue("https://wateroffice.ec.gc.ca/report/real_time_e.html?",
                   "stn={stn}&mode=Table&startDate={format(from, '%Y-%m%-%d')}&",
                   "endDate={format(to, '%Y-%m%-%d')}&",
                   "prm1=46&y1Max=&y1Min=&prm2=47&y2Max=&y2Min=#wb-auto-4")
download_url <- paste0("https://wateroffice.ec.gc.ca/download/report_e.html?",
                       "dt=6&df=csv&ext=zip")

cookie_path <- tempfile()
result_path <- tempfile(fileext = ".zip")

select_req <- request(select_url) |>
  req_cookie_preserve(cookie_path) |>
  req_perform() 

download_req <- request(download_url) |>
  req_cookie_preserve(cookie_path) |>
  req_perform()

download_req |>
  resp_body_raw() |>
  writeBin(result_path)

td <- tempdir()
fn <- unzip(result_path, list = TRUE)[, "Name"]
unzip(result_path, exdir = td)

(dat <- read_csv(file.path(td, fn),
         skip = 9))
                                                                             
# Rows: 34 Columns: 3
# ── Column specification ────────────────────────────────────────────────────────
# Delimiter: ","
# dbl  (2): Parameter, Value (m³/s)
# dttm (1): Date (MST)

# ℹ Use `spec()` to retrieve the full column specification for this data.
# ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# # A tibble: 34 × 3
#    `Date (MST)`        Parameter `Value (m³/s)`
#    <dttm>                  <dbl>          <dbl>
#  1 2024-06-20 00:00:00         6          19.7 
#  2 2024-06-21 00:00:00         6          15.7 
#  3 2024-06-22 00:00:00         6          13   
#  4 2024-06-23 00:00:00         6          11.1 
#  5 2024-06-24 00:00:00         6           9.93
#  6 2024-06-25 00:00:00         6          15.5 
#  7 2024-06-26 00:00:00         6          21.3 
#  8 2024-06-27 00:00:00         6         103   
#  9 2024-06-28 00:00:00         6         165   
# 10 2024-06-29 00:00:00         6          80.6 
# # ℹ 24 more rows
# # ℹ Use `print(n = ...)` to see more rows
© www.soinside.com 2019 - 2024. All rights reserved.