首页 > 解决方案 > 登录后下载 CSV 文件

问题描述

问题:

登录网站后如何从以下链接下载 CSV 文件?

到目前为止我所拥有的:

rg.headers <- c('User-Agent' = 'Mozilla/5.0')
rg.url <- "https://rotogrinders.com/"
rg.session <- html_session(rg.url, httr::add_headers(.headers=rg.headers))
rg.session <- rvest:::request_POST(rg.session, url = "https://rotogrinders.com/sign-in",
                                  body = list("username"="*****",
                                              "password"="*****"),
                                  encode = 'json')
bat.p_redirect <- jump_to(rg.session, "https://rotogrinders.com/grids/standard-projections-the-bat-x-3372510?site=draftkings")

它让我登录并将我重定向到带有“下载为 CSV ”按钮的页面。我只是不知道从这里到哪里去实际单击下载按钮并将 .csv 文件保存在我希望保存的位置。

标签: rcsvdownloadclickrvest

解决方案


我回答了我自己的问题。下载为 CSV按钮有自己的URL,因此我没有重定向到上面代码中的页面,而是重定向到按钮的 URL 并从那里抓取。

bat.p_redirect = jump_to(rg.session, "https://rotogrinders.com/grids/standard-projections-the-bat-x-3372510.csv?site=draftkings")$response$content
writeBin(bat.p_redirect, "C:/.../BAT.P.csv")

如果有人有更有效的解决方案,我很想听听,但这种方式对我来说效果很好。


推荐阅读