【问题标题】:Rvest Could not find possible submission target when submitting formRvest 提交表单时找不到可能的提交目标
【发布时间】:2017-01-23 19:24:00
【问题描述】:

我正在尝试从需要提交表单的站点中抓取结果,为此我使用的是 rvest 包。

运行以下命令后代码失败:

require("rvest")
require(dplyr)
require(XML)

BasicURL <- "http://www.blm.mx/es/tiendas.php"
QForm <- html_form(read_html(BasicURL))[[1]]
Values <- set_values(QForm, txt_5 = 11850, drp_1="-1")
Session <- html_session(BasicURL)
submit_form(session = Session,form = Values)

错误:找不到可能的提交目标。

我认为这可能是因为 rvest 没有找到用于提交的标准按钮目标。是否可以指定要查找哪些标签或按钮?

非常感谢任何帮助

【问题讨论】:

    标签: r xml dplyr form-submit rvest


    【解决方案1】:

    你可以直接用httrPOST到表格:

    library(httr)
    library(rvest)
    library(purrr)
    library(dplyr)
    
    res <- POST("http://www.blm.mx/es/tiendas.php",
                body = list(txt_5 = "11850", 
                            drp_1 = "-1"), 
                encode = "form")
    
    pg <- read_html(content(res, as="text", encoding="UTF-8"))
    
    map(html_nodes(pg, xpath=".//div[@class='tiendas_resultado_right']"), ~html_nodes(., xpath=".//text()")) %>% 
      map_df(function(x) {
        map(seq(1, length(x), 2), ~paste0(x[.:(.+1)], collapse="")) %>% 
          trimws() %>% 
          as.list() %>% 
          setNames(sprintf("x%d", 1:length(.)))
      }) -> right
    
    left <- html_nodes(pg, "div.tiendas_resultado_left") %>%  html_text()
    
    df <- bind_cols(data_frame(x0=left), right)
    
    glimpse(df)
    ## Observations: 7
    ## Variables: 6
    ## $ x0 <chr> "ABARROTES LA GUADALUPANA", "CASA ARIES", "COMERCIO RED QIUBO", "FERROCARRIL 4", "LA FLOR DE JALISCO", "LA MIGAJA", "VIA LACTEA"
    ## $ x1 <chr> "Calle IGNACIO ESTEVA", "Calle PARQUE LIRA", "Calle GENERAL JOSE MORAN No 74 LOCAL B", "Calle MELCHOR MUZQUIZ", "Calle MELCHOR M...
    ## $ x2 <chr> "Col. San Miguel Chapultepec I Sección", "Col. San Miguel Chapultepec I Sección", "Col. San Miguel Chapultepec I Sección", "Col....
    ## $ x3 <chr> "Municipio/Ciudad Miguel Hidalgo", "Municipio/Ciudad Miguel Hidalgo", "Municipio/Ciudad Miguel Hidalgo", "Municipio/Ciudad Migue...
    ## $ x4 <chr> "CP 11850", "CP 11850", "CP 11850", "CP 11850", "CP 11850", "CP 11850", "CP 11850"
    ## $ x5 <chr> "Estado Distrito Federal", "Estado Distrito Federal", "Estado Distrito Federal", "Estado Distrito Federal", "Estado Distrito Fed...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-26
      • 2019-01-31
      • 2018-11-24
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多