使用 rvest 登录雅虎 [英] Yahoo login using rvest

查看:69
本文介绍了使用 rvest 登录雅虎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,雅虎将他们的身份验证机制更改为两步一.所以现在,当我登录雅虎网站时,我输入了我的用户名,然后它要求我打开雅虎移动应用程序并为其提供代码.或者,您可以通过电子邮件或短信以其他方式解决此问题.这样做的结果是,过去用于以编程方式登录雅虎网站的代码不再有效.此代码只是重定向到登录表单.我试过在表单值中使用和不使用用户代理字符串以及使用和不使用 countrycode=1 .在查看我的移动应用程序后,我可以输入代码,但它不会将我转到输入该代码的页面.这些天我们如何使用 R 登录雅虎?

Recently, Yahoo changed their authentication mechanism to a two step one. So now, when I login to a yahoo site, I put in my username, and then it asks me to open my yahoo mobile app to give it a code. Alternatively, you can have it email or text you some other way around this. The result of this is that code that used to work to programatically login to Yahoo sites no longer works. This code just redirects to the login form. I've tried with and without a useragent string and with and without the countrycode=1 in the form values. I'm fine with entering a code after looking at my mobile app, but it doesn't forward me to the page to enter that code. How do we login to Yahoo these days using 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))

推荐答案

好的,我在这里偶然发现了答案.我使用 httr::add_headers("Content-Length"=0) 来响应 rvest 会抛出的警告:警告消息:在 request_POST(session, url = url, body = request$values, encode = request$encode, :长度要求(HTTP 411).

Okay, I've stumbled upon the answer here. I was using the httr::add_headers("Content-Length"=0) in response to a warning that rvest would throw: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Length Required (HTTP 411).

事实证明,尽管有警告,但一切正常,事实上,如果我添加 content-length 标头,登录就会失败.所以,我登录雅虎的代码最终看起来像这样:

As it turns out, despite the warning, everything worked fine and in fact, if I add the content-length header, the login fails. So, my code to login to yahoo ends up looking like this:

  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)

这是一个两步过程...提交您的用户名,然后转到您的雅虎应用程序以获取登录代码.不需要雅虎密码.我使用 readline 来获取登录代码.似乎运行良好...完成登录后,我可以抓取我的幻想足球数据.非常奇怪的是,要求内容长度标头的警告会引导您走上一条不起作用的道路.顺便说一下,当尝试登录谷歌时,同样的情况也适用.你必须忽略警告,它工作正常.

It's a two step process... Submit your username, then go to your yahoo app to get the login code. There's no yahoo password needed. I use readline to get the login code. Seems to work well... I'm able to scrape my fantasy football data after completing the login. It's just very curious that the warning asking for a content length header would lead you down a path that doesn't work. By the way, this same situation applies when trying to login to google. You have to ignore the warning and it works fine.

这篇关于使用 rvest 登录雅虎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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