2016-09-15 44 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