簡體   English   中英

R-使用RCurl發布登錄表單

[英]R - posting a login form using RCurl

我不熟悉使用R發布表單,然后從網絡下載數據。 我有一個問題,很可能很容易讓外面的人發現我在做什么錯,因此,感謝您的耐心配合。 我有一台Win7 PC,而Firefox 23.x是我的典型瀏覽器。

我正在嘗試發布顯示在

http://www.aplia.com/

我有以下R腳本:

your.username <- 'username'
your.password <- 'password'
setwd( "C:/Users/Desktop/Aplia/data" )

require(SAScii) 
require(RCurl)
require(XML)
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
)

# list parameters to pass to the website (pulled from the source html)
params <-
list(
'userAgent' = agent,
'screenWidth' = "",
'screenHeight' = "",
'flashMajor' = "",
'flashMinor' = "",
'flashBuild' = "",
'flashPatch' = "",
'redirect' = "",
'referrer' = "http://www.aplia.com",
'txtEmail' = your.username,
'txtPassword' = your.password 
    )

# logs into the form
html = postForm('https://courses.aplia.com/', .params = params, curl = curl)
html

# download a file once form is posted
html <-
getURL(
"http://courses.aplia.com/af/servlet/mngstudents?ctx=filename" ,
curl = curl
)
html

但是從那里我可以看出我沒有得到想要的頁面,因為返回到html的是重定向消息,該消息似乎要求我再次登錄(?):

"\r\n\r\n<html>\r\n<head>\r\n    <title>Aplia</title>\r\n\t<script language=\"JavaScript\" type=\"text/javascript\">\r\n\r\n        top.location.href = \"https://courses.aplia.com/af/servlet/login?action=form&redirect=%2Fservlet%2Fmngstudents%3Fctx%3Dfilename\";\r\n    \r\n\t</script>\r\n</head>\r\n<body>\r\n    Click <a href=\"https://courses.aplia.com/af/servlet/login?action=form&redirect=%2Fservlet%2Fmngstudents%3Fctx%3Dfilename\">here</a> to continue.\r\n</body>\r\n</html>\r\n"

盡管我確實相信成功將表單發布(手動,在瀏覽器中)后會發生一系列重定向。 我怎樣才能知道表格已正確張貼?

我非常確定,一旦我可以使帖子正常工作,指導R下載我需要的文件就不會有問題(本學期每500名學生的在線活動報告)。 但是花了幾個小時來解決這個問題並陷入困境。 也許我需要通過RCurl軟件包設置更多與cookie有關的選項(因為該站點確實使用cookie)-?

非常感謝任何幫助! 我通常使用R來處理統計數據,因此對於這些軟件包和功能來說是新手。

答案最終很簡單。 由於某種原因,我沒有看到postForm需要包含一個選項:

html = postForm('https://courses.aplia.com/', .params = params, curl = curl, style="POST")

就是這樣...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM