【问题标题】:R: Error in `html_form_submit()`: `form` doesn't contain a `action` attributeR: `html_form_submit()` 错误:`form` 不包含 `action` 属性
【发布时间】:2023-01-21 10:32:47
【问题描述】:

我正在尝试自动下载此处包含的数据: https://www.offenerhaushalt.at/gemeinde/innsbruck/download

我可以很容易地通过 url 指定表单: https://www.offenerhaushalt.at/gemeinde/innsbruck/download?year=2022&haushalt=fhh&rechnungsabschluss=va&origin=gemeinde

或通过 rvest 函数 html_form(),但我无法下载表单,因为 html_form_submit() 抛出错误:

Error in `submission_build()`:
! `form` doesn't contain a `action` attribute
library(rvest)
library(tidyverse)
html_form(read_html("https://www.offenerhaushalt.at/gemeinde/innsbruck/download"))[[1]] %>% 
    html_form_set(year = "2022", 
                  haushalt = "fhh",
                  rechnungsabschluss = "va",
                  origin = "gemeinde") %>% 
    html_form_submit()

关于如何捕获之后生成的文件并下载它的任何想法?

在我看来,它将“操作”发送到一个如下所示的网址:https://www.offenerhaushalt.at/downloads/ghdByParams

但我不确定该怎么做。

谢谢大家!

【问题讨论】:

  • 表单的操作似乎是由在该页面上运行的 javascript 设置的。 rvest 无法为您运行 javascript 代码。如果您需要运行 javascript,您可以使用 RSelenium 之类的东西,或者您可以使用开发人员工具检查请求并尝试使用 httrhttr2 之类的库重新创建它

标签: r web-scraping rvest


【解决方案1】:

您可以手动设置该表单的操作 url:

library(rvest)
library(purrr)
dl_url <- "https://www.offenerhaushalt.at/gemeinde/innsbruck/download"

sess <- session(dl_url)
form <- sess %>% read_html() %>% html_form() %>% .[[1]]

# list valid options for select boxes
map(form$fields, "options") %>% keep(~ length(.x) > 0) %>% 
  imap_dfr(~ list(field = .y, options = paste(.x, collapse = " ")))
#> # A tibble: 4 × 2
#>   field              options                                                    
#>   <chr>              <chr>                                                      
#> 1 haushalt           default fhh ehh vhh                                        
#> 2 rechnungsabschluss default ra va                                              
#> 3 year               default 2022 2021 2020 2019 2018 2017 2016 2015 2014 2013 …
#> 4 origin             default statistik_at gemeinde

# set values
form$fields$haushalt$value <- "fhh"
form$fields$rechnungsabschluss$value <- "ra"
form$fields$year$value <- "2020"
form$fields$origin$value <- "statistik_at"

# manually set form method & action
form$method <- "POST"
form$action <- "https://www.offenerhaushalt.at/downloads/ghdByParams"

# submit form
sess <- session_submit(sess, form)

# response headers
imap_dfr(sess$response$headers, ~ list(header = .y, value = .x))
#> # A tibble: 10 × 2
#>    header              value                                                    
#>    <chr>               <chr>                                                    
#>  1 date                Sat, 21 Jan 2023 01:47:13 GMT                            
#>  2 server              Apache                                                   
#>  3 content-disposition attachment; filename=offenerhaushalt_70101_2020_ra_fhh.c…
#>  4 pragma              no-cache                                                 
#>  5 cache-control       must-revalidate, post-check=0, pre-check=0, private      
#>  6 expires             0                                                        
#>  7 set-cookie          XSRF-TOKEN=eyJpdiI6IjdHd2pSakwzV09xb3Jab05zXC81em1RPT0iL…
#>  8 set-cookie          offener_haushalt_session=eyJpdiI6IjI5cUN5MGhCSmVadmN5enV…
#>  9 transfer-encoding   chunked                                                  
#> 10 content-type        text/csv; charset=UTF-8

# parse attached CSV
httr::content(sess$response, as = "text") %>% readr::read_csv2()
#> ℹ Using "','" as decimal and "'.'" as grouping mark. Use `read_delim()` for more control.
#> Rows: 1408 Columns: 11
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ";"
#> chr (8): ansatz_uab, ansatz_ugl, konto_grp, konto_ugl, sonst_ugl, vorhabenco...
#> dbl (2): mvag, wert
#> lgl (1): verguetung
#> 
#> ℹ 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: 1,408 × 11
#>    ansat…¹ ansat…² konto…³ konto…⁴ sonst…⁵ vergu…⁶ vorha…⁷  mvag ansat…⁸ konto…⁹
#>    <chr>   <chr>   <chr>   <chr>   <chr>   <lgl>   <chr>   <dbl> <chr>   <chr>  
#>  1 000     000     042     000     000     NA      0000000  3415 Gewähl… Amts-,…
#>  2 000     000     070     000     000     NA      0000000  3411 Gewähl… Aktivi…
#>  3 000     000     400     000     000     NA      0000000  3221 Gewähl… Gering…
#>  4 000     000     413     000     000     NA      0000000  3221 Gewähl… Handel…
#>  5 000     000     456     000     000     NA      0000000  3221 Gewähl… Schrei…
#>  6 000     000     457     000     000     NA      0000000  3221 Gewähl… Druckw…
#>  7 000     000     459     000     000     NA      0000000  3221 Gewähl… Sonsti…
#>  8 000     000     618     000     000     NA      0000000  3224 Gewähl… Instan…
#>  9 000     000     621     000     000     NA      0000000  3222 Gewähl… Sonsti…
#> 10 000     000     631     000     000     NA      0000000  3222 Gewähl… Teleko…
#> # … with 1,398 more rows, 1 more variable: wert <dbl>, and abbreviated variable
#> #   names ¹​ansatz_uab, ²​ansatz_ugl, ³​konto_grp, ⁴​konto_ugl, ⁵​sonst_ugl,
#> #   ⁶​verguetung, ⁷​vorhabencode, ⁸​ansatz_text, ⁹​konto_text

创建于 2023-01-21 reprex v2.0.2

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 2011-07-21
    • 2015-04-26
    • 1970-01-01
    • 2016-08-24
    • 2018-11-17
    • 1970-01-01
    • 2021-01-30
    • 2013-10-29
    相关资源
    最近更新 更多