gpt4 book ai didi

Rvest 提交表单时找不到可能的提交目标

转载 作者:行者123 更新时间:2023-12-02 20:58:21 28 4
gpt4 key购买 nike

我正在尝试从需要提交表单的网站中抓取结果,为此我使用 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 找不到用于提交的标准按钮目标。是否可以指定要查找的标签或按钮?

非常感谢任何帮助

最佳答案

您可以使用httr直接POST到表单:

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

关于Rvest 提交表单时找不到可能的提交目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39516673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com