使用httr R包发送POST请求 [英] Send a POST request using httr R package

查看:406
本文介绍了使用httr R包发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过POST请求发送服务器数据时,我不知道如何模仿浏览器的功能.这是相关的URL,下面有解释.

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)主页(未登录时选择团队页面,重定向->(2))

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

(2)登录页面(成功登录后将重定向到特定于团队的页面)

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

(3)特定于团队的页面:例如米饭

(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>

最终希望使用rvest软件包抓取一些信息,但最终结果为空,因为它试图抓取:

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

推荐答案

尝试

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())

这篇关于使用httr R包发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆