使用R登录时使用隐藏的csrf令牌抓取网站

时间:2016-01-07 21:33:37

标签: r rcurl

作为深夜项目的一部分,我正在努力工作,我正试图从他们的网站上删除我的Uber旅行数据。

我已经看过

上登录页面的代码了
  

https://login.uber.com/login

并且已经看到他们在表单设置中使用POST方法如下:

<form method="post" class="form" novalidate="">
<input type="hidden" name="_csrf_token" value="1452201446-01-hujzoBTxkYPrJessd6zQwnD2ZOFxMOVgIYN8iXntr6c=">
<input type="hidden" data-js="access-token" name="access_token">
  <a href="#" class="btn btn--full btn--facebook" data-js="facebook-connect">
      <span class="push--ends flush">Continue with Facebook</span>
  </a>
  <p class="primary-font primary-font--semibold text-uber-white background-line push--top push--bottom">
    <span>or use email</span>
  </p>

<div class="form-group push-tiny--top flush--bottom">
  <input type="email" name="email" class="text-input square--bottom " placeholder="Email Address" value="" id="email">
</div>
<div class="form-group push--bottom">
  <input type="password" name="password" class="text-input square--top " placeholder="Password" id="password">
</div>

我读到的是,在尝试刮取

时需要发送csrf令牌
library(RCurl)
library(XML)

URL_str<-"https://login.uber.com/login"
URL_str2<-"https://riders.uber.com/trips"

email<-"exampleMail@gmail.com"
pass<-"thisisalong"
token<- "1452201446-01-hujzoBTxkYPrJessd6zQwnD2ZOFxMOVgIYN8iXntr6c="

params <- list('email' = email,
               'password' = pass,
               '_csrf_token'=token)

URL_doc = postForm(URL_str2, style="POST",
                   .params=params)

如果我现在尝试抓住网站,我会

ERROR: FORBIDDEN

我在python中看到过类似网站的一些例子。可以在R中完成同样的工作吗?

1 个答案:

答案 0 :(得分:0)

最后的答案最终很简单:

library(rvest)
library(RCurl)
library(XML)

session <-html_session("https://login.uber.com/login")

email<-"exampleMail@gmail.com"
pass<-"thisisalong"

#Handling the html_form
form<-html_form(session)[[1]]
form<-set_values(form, email=email, password=pass)

#Found that unless I explicitly assigned the value to empty it created errors
form$url<-""

session_open<-submit_form(session,form)

session_open<-jump_to(session_open,URL_str2)

从此处开始使用html_table直接拉动表并使用html_nodes隔离节点。希望这有帮助