好吧,我在这里偶然发现了答案。我是在响应警告rvest
会抛出使用httr::add_headers("Content-Length"=0)
:Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Length Required (HTTP 411).
事实证明,尽管警告,一切正常,事实上,如果我添加的Content-Length头,登录失败。所以,我的代码登录到雅虎最终看起来像这样:
username <- "[email protected]"
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
来获取登录码。似乎运作良好......完成登录后,我可以抓取我的幻想足球数据。只是非常好奇的是,要求提供内容长度标题的警告会导致你失去一条不起作用的路径。顺便说一下,当尝试登录到Google时,这种情况也适用。你必须忽略这个警告,它工作正常。