Send a POST request using httr R package

自作多情 提交于 2020-08-22 03:49:19

问题


I can't figure out how to imitate what the browser does, when sending the server data via a POST request. Here are the relevant URLs with explanation below.

(1) http://kenpom.com/

(2) http://kenpom.com/register.php?frompage=1
<form id="login" method="POST" action="handlers/login_handler.php">
<label>E-mail </label><input type="text" name="email" />
<label>Password </label><input type="password" name="password" />
<input type="submit" name="submit" value="Login!" />

(3) http://kenpom.com/team.php?team=Rice

(1) home page (select team page when NOT logged in, re-direct -> (2) )

(2) login page (re-direct to team specific page upon successful login)

(3) team specific page: e.g. Rice

url <- ("http://kenpom.com/team.php?team=Rice")

login <- list(
        email = "login",
        password = "password"
)

teampage <- POST(url, body = login)

Response [http://kenpom.com/register.php?frompage=1]
  Date: 2015-03-07 23:04
  Status: 200
  Content-Type: text/html
  Size: 7.45 kB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<LINK REL=stylesheet TYPE="text/css" HREF="css/rate.css?1414365416">
<TITLE>kenpom.com subscription</TITLE>

Ultimately want to scrape some info using the rvest package but end up with empty results as it attempts to scrape: http://kenpom.com/register.php?frompage=1


回答1:


Try

library(httr)
login <- list(
  email = "login",
  password = "password",
  submit = "Login!"
)
res <- POST("http://kenpom.com/handlers/login_handler.php", body = login, encode = "form", verbose())
team <- GET("http://kenpom.com/team.php?team=Rice", verbose())


来源:https://stackoverflow.com/questions/28924820/send-a-post-request-using-httr-r-package

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