如何使用R填写html表单并下载结果文件?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (44)

我有一份500多个(可能更多)气象站的列表,我想从这个网站下载FW 13格式的数据(https://fam.nwcg.gov/fam-web/kcfast/html/wxhmenu.htm)在浏览器中,你填写表单,提交它,它将开始下载FW 13文件到我的默认下载文件夹。

我的目标是使用R填写html表单,提交它,然后接受将结果文件下载到定义的位置。表单本身由文本和单选按钮组成。下面是一个查询的示例:

站号:020207

开始日期:2000-01-01

截止日期:2017-12-31

观察类型:每小时

调度选项:立即运行它

我去了rabbit holes 的Rcurl ,甚至开始尝试rSelu。我看到的大多数例子是直接从网站上抓取信息,但我只是想接受下载的结果文件。

如果我只需提交一个请求,并下载一个文件,我相信我可以弄清楚如何使用一个站点ID列表来实现我所需要的。

提问于
用户回答回答于
library(httr)
library(tidyverse)

POST(
  url = "https://fam.nwcg.gov/FAMCognosRESTServices/rest/cognos/anonymous/reports/FW13",
  encode = "json",
  body = list(
    p_end_date = "2017-12-31",
    p_obs_type = "Hourly",
    p_start_date = "2000-01-01",
    p_station_id = "170701",
    reportName = "",
    reportSched = FALSE,
    reportTime = ""
  )
) -> res

out <- content(res)

readr::read_fwf(
  file = out$report, 
  fwf_widths(
    widths = c(3, 6, 8, 4, 1, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 5, 1, 2, 2, 1, 1, 1, 4, 3, 3, 1),
    col_names = c("w13", "sta_id", "obs_dt", "obs_tm", "obs_type", "sow", "dry_temp", "rh", 
                  "wind_dir", "wind_sp", "fuel_10hr", "temp_max", "temp_min", "rh_max", 
                  "rh_min", "pp_dur", "pp_amt", "wet", "grn_gr", "grn_sh", "moist_tpe", 
                  "meas_type", "season_cd", "solar_radiation", "wind_dir_peak",
                  "wind_speed_peak", "snow_flg")
  ),
  skip = 1
) %>% 
  glimpse()
## Observations: 1,059
## Variables: 27
## $ w13             <chr> "W13", "W13", "W13", "W...
## $ sta_id          <int> 170701, 170701, 170701,...
## $ obs_dt          <int> 20000102, 20000103, 200...
## $ obs_tm          <int> 1300, 1300, 1300, 1300,...
## $ obs_type        <chr> "O", "O", "O", "O", "O"...
## $ sow             <int> 3, 1, 4, 0, 1, 5, 1, 7,...
## $ dry_temp        <int> 44, 40, 48, 26, 25, 37,...
## $ rh              <int> 66, 50, 100, 51, 53, 93...
## $ wind_dir        <int> 230, 360, 230, 360, 120...
## $ wind_sp         <int> 2, 10, 13, 14, 5, 0, 9,...
## $ fuel_10hr       <int> NA, NA, NA, NA, NA, NA,...
## $ temp_max        <int> 48, 52, 51, 54, 29, 37,...
## $ temp_min        <int> 10, 36, 28, 23, 8, 22, ...
## $ rh_max          <int> 100, 100, 100, 100, 80,...
## $ rh_min          <int> 60, 50, 100, 51, 38, 93...
## $ pp_dur          <int> 0, 0, 13, 8, 0, 8, 0, 0...
## $ pp_amt          <int> 0, 0, 150, 1020, 0, 100...
## $ wet             <chr> "N", "N", "Y", "Y", "N"...
## $ grn_gr          <chr> NA, NA, NA, NA, NA, NA,...
## $ grn_sh          <chr> NA, NA, NA, NA, NA, NA,...
## $ moist_tpe       <int> 2, 2, 2, 2, 2, 2, 2, 2,...
## $ meas_type       <int> 1, 1, 1, 1, 1, 1, 1, 1,...
## $ season_cd       <chr> NA, NA, NA, NA, NA, NA,...
## $ solar_radiation <chr> NA, NA, NA, NA, NA, NA,...
## $ wind_dir_peak   <chr> NA, NA, NA, NA, NA, NA,...
## $ wind_speed_peak <chr> NA, NA, NA, NA, NA, NA,...
## $ snow_flg        <chr> "N", "N", "N", "N", "N"...

扫码关注云+社区

领取腾讯云代金券