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

时间:2018-08-24 17:42:55

标签: r rvest httr

我想提交以下表单(单击链接“ 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')

最后一行产生错误:

  

错误:未知的提交名称“ submit”。可能的值:   clanica.asTextDatumGasenjaTo,clanica.asTextUdr_id

我不明白为什么rvest在不包含提交名称或类型的情况下将这两个参数识别为提交按钮。为何rvest无法将提交按钮“ Trazi”识别为提交参数?简而言之,如何更改已填写的表单以执行表单?

1 个答案:

答案 0 :(得分:7)

问题在于某些输入缺少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,并在this PR中提出了修复程序,但是自2016年7月以来尚未合并,因此请不要屏住呼吸。

PR中的修复程序基本上检查是否存在type attr:

  # 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创建。