rvest: “unknown field names” when attempting to se

2019-07-23 05:56发布

问题:

I'm attempting to generate a web form to allow me to scrape data.

library(rvest)

url <- "https://iemweb.biz.uiowa.edu/pricehistory/pricehistory_SelectContract.cfm?market_ID=214"

pg.form <- html_form(html(url))

which returns

pg.form
[[1]]
<form> '<unnamed>' (POST PriceHistory_GetData.cfm)
 <input HIDDEN> 'Market_ID': 214
 <select> 'Month' [1/12]
 <select> 'Year' [0/2]
 <input SUBMIT> '': Get Prices

My mistake is to think that I need to set values for the Month and Year fields, but this is a mistake

filled_form <- set_values(pg.form,
                          Month = "8",
                          Year = "0")

returns Error: Unknown field names: Month, Year

How do I use rvest to set values in a webform?

回答1:

From your output, pg.form is actually a list forms rather than a single form. To access the first form either do

set_values(pg.form[[1]], Month="8")

or you can do

pg.form <- html_form(html(pg.session))[[1]]

instead.



回答2:

lnk3 <- 'http://data.nowgoal.com/history/handicap.htm' #this website content includes the odds price
> sess <- html_session(lnk3)
> f0 <- sess %>% html_form
> f1 <- set_values(f0[[2]], matchdate=dateID[1], companyid1=list(c(3,8,4,12,1,23,24,17,31,14,35,22)))
Warning message:
Setting value of hidden field 'companyid1'. 
> s <- submit_form(sess, f1)
Submitting with 'NULL'

Tried to submit an form which is hidden field but sounds doen't work, subnmitting with 'NULL'