Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

r - rvest: how to submit form when input doesn't have a name?

I have a simple problem and I don't know how to settle it. I want to fill a form using rvest where the input have no name:

library(rvest)
session <- html_session("https://www.tripadvisor.com/")
pgform <- html_form(session)[[1]]

> pgform
<form> 'global_nav_search_form' (GET /Search)
  <input search> '': 
  <input text> '': 
  <button submit> 'sub-search
  <input hidden> 'geo': 1
  <input hidden> 'latitude': 
  <input hidden> 'longitude': 
  <input hidden> 'searchNearby': 
  <input hidden> 'pid': 3826
  <input hidden> 'redirect': 
  <input hidden> 'startTime': 
  <input hidden> 'uiOrigin': 
  <input hidden> 'q': 
  <input hidden> 'supportedSearchTypes': find_near_stand_alone_query
  <input hidden> 'enableNearPage': true
  <input hidden> 'returnTo': __2F__
  <input hidden> 'searchSessionId': C9C09F9043AE6FE69CE679DF8A44546D1547136702473ssid
  <input hidden> 'social_typeahead_2018_feature': true

Here I would like to do a search by setting the input text, to have the link of the page. Of course if I do

filledform <- set_values(pgform, '' = "Paris")

I have an error :

Error: attempt to use zero-length variable name

I am sure there is a simple workaround, but I don't know it. Any idea ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Modifying empty fields

You can access and modify a field with an empty name directly by using the field's index, for example like this:

pgform$fields[[2]]$value <- 'Paris'

If you want to find the index of the field dynamically by its type, you could do that like this:

for (i in 1:length(pgform$fields))
    if (is.null(pgform$fields[[i]]$name) && pgform$fields[[i]]$type == 'text')
        pgform$fields[[i]]$value <- 'Paris'

Your specific problem

For your specific website, the above will not give you the expected results. The field you need to modify to submit a query is q, so you would want to do something like this:

session <- html_session('https://www.tripadvisor.com/')
pgform <- html_form(session)[[1]]
pgform <- set_values(pgform, q = 'Paris')
result <- submit_form(session, pgform)

This will load the desired page for you but will not provide you with the content you are probably looking for, as that content would only be loaded dynamically by the browser using a XMLHttpRequest. To also get the content you would instead need to do something like this:

session <- html_session('https://www.tripadvisor.com/')
pgform <- html_form(session)[[1]]
pgform <- set_values(pgform, q = 'Paris')
result <- submit_form(session, pgform, submit = NULL, httr::add_headers('x-requested-with' = 'XMLHttpRequest'))

That will give you the content without the surrounding page structure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...