geekzeus
geekzeus

Reputation: 895

Add name to set_values using Rvest

I am trying to scrape this website, I have written a code for login using rvest, but every time the page refreshes the name of the form changes.

library(rvest)

loginpage <- "https://demo.glpi-project.org/"

pagesession <- html_session(loginpage)

pageform <- html_form(pagesession)[[1]]

formfill <- set_values(pageform, fielda5bd99dcd2eaa8 = "****", 
fieldb5bd99dcd2eaad = "****")

successlogin <- submit_form(pagesession,formfill)

fielda5bd99dcd2eaa8 & fieldb5bd99dcd2eaad are the name of the input field which changes every time it refreshes.

right now I am changing the name field every time I run the script

Upvotes: 0

Views: 103

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78832

Hopefully this is enough of a hint to get you in the right direction:

library(rvest)
library(httr)
library(dplyr)

httr::GET(
  "https://demo.glpi-project.org/"
) -> res

pg <- httr::content(res)

form <- html_nodes(pg, "form")
inputs <- html_nodes(form, "input")

data_frame(
  id = html_attr(inputs, "id"),
  name = html_attr(inputs, "name"),
  value = html_attr(inputs, "value")
)
## # A tibble: 6 x 3
##   id             name                value                           
##   <chr>          <chr>               <chr>                           
## 1 login_name     fielda5bd9bf41b7af9 NA                              
## 2 login_password fieldb5bd9bf41b7afe NA                              
## 3 NA             auth                local                           
## 4 login_remember fieldc5bd9bf41b7aff NA                              
## 5 NA             submit              Post                            
## 6 NA             _glpi_csrf_token    ea1aff0b53753e14a76077bd77fb21c2

Upvotes: 1

Related Questions