How to post within a rvest html_session?

南笙酒味 提交于 2019-12-09 23:04:48

问题


How can i post "within" a html session?

So after i opened a session via a <- rvest::html_session(url)

I tried:

library(httr)
POST(path, 
          add_headers(setNames(as.character(headers(a)), names(headers(a)))), 
          set_cookies(setNames(cookies(a)$value, cookies(a)$name)),
          body = list(...), 
          encode = "json")

But this handles my request as I were not logged in. Any suggestions? I am looking for something like POST(session, path, body, ...)


回答1:


Ok, after some digging into it i solved it by using:

x %>% rvest:::request_POST(url,
          config(referer = x$url),
          user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36"),
          body = list(...), 
          encode = "form")

Where rvest:::request_POST internally uses

httr::POST(url, x$config, ..., handle = x$handle)


来源:https://stackoverflow.com/questions/37085732/how-to-post-within-a-rvest-html-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!