2016-09-15 60 views
0

我试图从需要表单提交的网站上刮取结果,为此我使用了rvest软件包。运行以下命令后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) 

Error: Could not find possible submission target.

我想可能是因为rvest没有找到标准按钮目标提交。有没有指定要寻找哪些标签或按钮?

任何帮助,不胜感激

回答

2

可以POST的形式直接与httr

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... 
+0

它的工作原理。谢谢! – eclark