Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rvest登录雅虎_R_Rvest_Httr - Fatal编程技术网

使用rvest登录雅虎

使用rvest登录雅虎,r,rvest,httr,R,Rvest,Httr,最近,雅虎将认证机制改为两步一步。所以现在,当我登录雅虎网站时,我输入了我的用户名,然后它要求我打开我的雅虎移动应用程序,给它一个代码。或者,你可以让它通过电子邮件或其他方式发短信给你。这样做的结果是,以前用于以编程方式登录雅虎网站的代码不再有效。这段代码只是重定向到登录表单。我尝试过使用和不使用useragent字符串,以及在表单值中使用和不使用countrycode=1。我可以在查看我的移动应用程序后输入代码,但它不会将我转发到页面以输入代码。现在我们如何使用R登录雅虎 url <-

最近,雅虎将认证机制改为两步一步。所以现在,当我登录雅虎网站时,我输入了我的用户名,然后它要求我打开我的雅虎移动应用程序,给它一个代码。或者,你可以让它通过电子邮件或其他方式发短信给你。这样做的结果是,以前用于以编程方式登录雅虎网站的代码不再有效。这段代码只是重定向到登录表单。我尝试过使用和不使用useragent字符串,以及在表单值中使用和不使用
countrycode=1
。我可以在查看我的移动应用程序后输入代码,但它不会将我转发到页面以输入代码。现在我们如何使用R登录雅虎

url <- "http://mail.yahoo.com"
uastring <- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

s <- rvest::html_session(url, httr::user_agent(uastring))
s_form <- rvest::html_form(s)[[1]]
filled_form <- rvest::set_values(s_form, username="myusername", 
                                 passwd="mypassword")
out <- rvest::submit_form(session=s, filled_form, submit="signin",
                          httr::add_headers("Content-Length"=0))

url好的,我在这里偶然找到了答案。我使用了
httr::add_headers(“Content Length”=0)
来响应
rvest
将抛出的警告:
警告消息:
在request_POST(会话,url=url,body=request$values,encode=request$encode,)中:
所需长度(HTTP 411)。

事实证明,尽管有警告,但一切正常,事实上,如果我添加内容长度标题,登录将失败。因此,我登录yahoo的代码最终如下所示:

  username <- "some_username@yahoo.com"
  league_id <- "some league id to complete the fantasy football url"

  uastring <- "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
  url <- "http://football.fantasysports.yahoo.com/f1/"
  url <- paste0(url, league_id)

  s <- rvest::html_session(url, httr::user_agent(uastring))  
  myform <- rvest::html_form(s)[[1]]
  myform <- rvest::set_values(myform, username=username)
  s <- suppressWarnings(rvest::submit_form(s, myform, submit="signin"))
  s <- rvest::jump_to(s, s$response$url)
  myform <- rvest::html_form(s)[[1]]
  if("code" %in% names(myform$fields)){
    code <- readline(prompt="In your Yahoo app, find and click on the Account Key icon.\nGet the 8 character code and\nenter it here: ")
  }else{
    print("Unable to login")
    return(NULL)
  }
  myform <- rvest::set_values(myform, code=code)  
  s <- suppressWarnings(rvest::submit_form(s, myform, submit="verify"))
  if(grepl("authorize\\/verify", s$url)){
    print("Wrong code entered, unable to login")
    return(NULL)
  }else{
    print("Login successful")
  }
  s <- rvest::jump_to(s, s$response$url)
用户名