-2

我正在尝试使用 R 登录http://etportfolio.economictimes.indiatimes.com/Login.htm以访问我在创建的投资组合中持有的股票。这是我收到消息“方法不允许”的脚本

library(RJSONIO)
library(rjson)
library(RCurl)

url <- ('http://etportfolio.economictimes.indiatimes.com/Login.htm')

agent="Firefox/23.0"
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
curl = getCurlHandle()
curlSetOpt( cookiejar = 'cookies.txt',  useragent = agent,  followlocation = TRUE ,  
autoreferer   = TRUE ,  curl = curl)


params <- list( 'ru' = "",
            'nru' = "",
            'loginPage' = "login",
            'channel' = "portfolio",
            'scriptTrackingCode' = "",
            'nonscriptTrackingCode' = "",
            'emailid' = "xxxx@hotmail.com",
            'password' = "password")

html1 = postForm(url,.params = params,curl = curl, style = "POST", verbose=TRUE)

如果我尝试使用 httr 包,我可以获得登录成功,但需要有关如何前进的帮助。这是代码。

library(httr)
base_url <- ("http://etportfolio.economictimes.indiatimes.com/")

loginURL <- modify_url(
base_url,
path="/loginfrm.htm",
query = list(
title = "loginform",
action = "http://socialappsintegrator.indiatimes.com/socialsite/GenricSSOLogin?",
type = "login",
emailid = "xxx@hotmail.com",
password = "password" ) )
r <- POST(loginURL)

您能否帮助确定我是否犯了错误,或者有没有办法可以使用 R 进行身份验证?

4

1 回答 1

1

通常,根据用例,我发现使用 selenium 驱动浏览器更容易。以下是填写表格的相当标准的脚本。

library(RSelenium)

appURL <- ('http://etportfolio.economictimes.indiatimes.com/Login.htm')
myUser <- "user"
myPass <- "pass"
RSelenium::checkForServer()
RSelenium::startServer()
remDr <- remoteDriver()
remDr$open()
remDr$navigate(appURL)
webElem <- remDr$findElement("id", "emailid")
webElem$sendKeysToElement(list(myUser))
webElem <- remDr$findElement("id", "password")
webElem$sendKeysToElement(list(myPass))
# submit element here rather then click
remDr$findElement('class', 'btn-login')$submitElement()
于 2014-09-11T20:38:48.003 回答