File download from filling a form

Asked

Viewed 231 times

3

I’m trying to access a site, fill out your form and download the file, but I’m encountering some difficulties.

That’s my code so far:

#library's
require(rvest)

#website
url <- ("http://www.anbima.com.br/est_termo/Curva_Zero.asp")
pgsession <- html_session(url)
pgform <-html_form(pgsession)[[1]]
param <- set_values(pgform, 
        "escolha" = "2",
        "Dt_Ref" = Sys.Date()
)

submit <- submit_form(pgsession, form = param, "Consultar")

But it returns an error when sending Submit: Erro: Could not find possible submission target.

Can someone help me?

  • The problem in Submit, eh that the form does not contain a button "Submit", here I solved the problem by creating a "fake Submit" in the form, but tested here and although the request worked, did not download the file... Probably need some other step to download

  • 1

    I put the code with Submit working on a gist, but as I said, I’m only getting the feedback from Submit and not a file for donwload. But the mistake you’re making in the question, it doesn’t happen anymore if you do this... If this helps and solves your problem, I will post this comment in response... https://gist.github.com/ibombonato/11507d776d1042f80ca59cd31509afd3

  • But do you have any idea how to pull this download? If it was on a site with the Ubmit button, what would you do? I’m looking for something here

  • 1

    Looking at the source code, has a javscript and I saw that the form needs to be changed and contain the url point to CZ-down.asp instead of Curva_zero.aspafter this, it seems to me that the file is returned in the "Answer" of the request in raw format, but I don’t know exactly how to convert it to excel :- I would have to do a search to find out if it is possible to do this... I imagine it is necessary to create a connection, read the file through it and then save the same.

  • Replaces . Curva_zero.Asp by CZ-down.Asp and using the download.file() function worked. It was much simpler than I imagined, but that’s it, thanks friend!

  • If possible, put the complete code as the answer, so others can see the entire solution you used.

Show 1 more comment

1 answer

0


As discussed with the friend above, it was much easier than imagined the result:

download.file("http://www.anbima.com.br/est_termo/CZ-down.asp", 
destfile = "")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.