当 rvest 无法识别提交按钮时提交 POST 表单

Submit POST form when rvest doesn't recognize submit button

我想提交以下表格(表格在您点击 link "Kliknite na ..." 后出现): http://www1.biznet.hr/HgkWeb/do/extlogon

我必须输入一个名为 "OIB" 的参数,然后单击 "Trazi" 提交表单。

这是我的代码:

library(httr)
library(rvest)

sess <- html_session("http://www1.biznet.hr/HgkWeb/do/extlogon")
search_page <- sess %>%
  follow_link(1)
form <- html_form(search_page)[[6]]
fill_form <- set_values(form, 'clanica.cla_oib' = '94989605030')
firma_i <- submit_form(search_page, fill_form, submit = 'submit')

最后一行产生错误:

Error: Unknown submission name 'submit'. Possible values: clanica.asTextDatumGasenjaTo, clanica.asTextUdr_id

我不明白为什么 rvest 在这两个参数不包含提交名称或类型时将它们识别为提交按钮。为什么 rvest 不能将提交按钮 "Trazi" 识别为提交参数?简而言之,如何更改已填写的表格以执行表格?

问题是某些输入缺少 type 属性,并且 rvest 没有正确检查它。

为了说明问题:

library(httr)
library(rvest)
#> Loading required package: xml2

sess <- html_session("http://www1.biznet.hr/HgkWeb/do/extlogon")
search_page <- sess %>%
  follow_link(1)
#> Navigating to /HgkWeb/do/extlogon;jsessionid=88295900F3F932C85A25BB18F326BE28
form <- html_form(search_page)[[6]]
fill_form <- set_values(form, 'clanica.cla_oib' = '94989605030')

有些字段没有type属性:

sapply(fill_form$fields, function(x) '['(x, 'type'))
#> $clanica.limitSearchToActiveCompany.type
#> [1] "radio"
#> 
#> $clanica.limitSearchToActiveCompany.type
#> [1] "radio"
#> 
#> $joinBy.useInnerJoin.type
#> [1] "checkbox"
#> 
#> $nazivTvrtke.type
#> [1] "text"
#> 
#> $nazivZapocinjeSaPredanomVrijednoscu.type
#> [1] "checkbox"
#> 
#> $clanica.cla_jmbp.type
#> [1] "text"
#> 
#> $clanica.cla_mbs.type
#> [1] "text"
#> 
#> $clanica.cla_oib.type
#> [1] "text"
#> 
#> $asTextKomoraId.NA
#> NULL
#> 
#> $clanica.asTextOpc_id.NA
#> NULL
#> 
#> $clanica.cla_opcina.type
#> [1] "hidden"
#> 
#> $clanica.asTextNas_id.NA
#> NULL
#> 
#> $clanica.cla_naselje.type
#> [1] "hidden"
#> 
#> $clanica.pos_id.NA
#> NULL
#> 
#> $clanica.postaNaziv.type
#> [1] "hidden"
#> 
#> $clanica.cla_ulica.type
#> [1] "text"
#> 
#> $clanica.asTextDatumUpisaFrom.type
#> [1] "text"
#> 
#> $clanica.asTextDatumUpisaTo.type
#> [1] "text"
#> 
#> $clanica.asTextDatumGasenjaFrom.type
#> [1] "text"
#> 
#> $clanica.asTextDatumGasenjaTo.type
#> [1] "text"
#> 
#> $clanica.asTextUdr_id.NA
#> NULL
#> 
#> $clanica.asTextVel_id.NA
#> NULL
#> 
#> $nkd2007.type
#> [1] "text"
#> 
#> $nkd2007PretrazivanjePoGlavnojDjelatnosti.type
#> [1] "radio"
#> 
#> $nkd2007PretrazivanjePoGlavnojDjelatnosti.type
#> [1] "radio"
#> 
#> $submit.type
#> [1] "submit"
#> 
#> $org.apache.struts.taglib.html.CANCEL.type
#> [1] "submit"
#> 
#> $orderBy.order1.NA
#> NULL
#> 
#> $orderBy.order2.NA
#> NULL
#> 
#> $limit.type
#> [1] "text"
#> 
#> $searchForRowCount.type
#> [1] "checkbox"
#> 
#> $joinBy.gfiGodina.NA
#> NULL
#> 
#> $joinBy.gfiBrojZaposlenihFrom.type
#> [1] "text"
#> 
#> $joinBy.gfiBrojZaposlenihTo.type
#> [1] "text"
#> 
#> $joinBy.gfiUkupniPrihodFrom.type
#> [1] "text"
#> 
#> $joinBy.gfiUkupniPrihodTo.type
#> [1] "text"

这弄乱了内部函数 submit_request,特别是其中的 Filter()


它被引用 here, and a fix is proposed in this PR,但它自 2016 年 7 月以来一直没有被合并,所以不要屏住呼吸。

PR 中的修复主要检查是否存在 type 属性:

  # form.R, row 280
  is_submit <- function(x) 'type' %in% names(x) &&
                           tolower(x$type) %in% c("submit", "image", "button")

快速修复您可以更改您拥有的数据,使用随机类型覆盖 NULL 属性:

fill_form$fields <- lapply(fill_form$fields, function(x) {
  null_type = is.null(x$type)
  if (null_type) x$type = 'text'
  x
})


firma_i <- submit_form(search_page, fill_form, submit = 'submit')
firma_i
#> <session> http://www1.biznet.hr/HgkWeb/do/fullSearchPost
#>   Status: 200
#>   Type:   text/html;charset=UTF-8
#>   Size:   4366

reprex package (v0.2.0) 创建于 2018-08-27。