4

在 Windows cURL 中,我可以发布与此类似的 Web 请求:

curl  --dump-header cook.txt ^
  --data "RURL=http=//www.example.com/r&user=bob&password=hello" ^
  --user-agent  "Mozilla/5.0"  ^
  http://www.example.com/login

我得到type cook.txt类似这样的回应:

HTTP/1.1 302 Found                                                 
Date: Thu, ******
Server: Microsoft-IIS/6.0                                          
SERVER: ******                                                  
X-Powered-By: ASP.NET                                              
X-AspNet-Version: 1.1.4322                                         
Location: ******
Set-Cookie: Cookie1=; domain=******; expires=****** ******
******
******
Cache-Control: private                                             
Content-Type: text/html; charset=iso-8859-1                        
Content-Length: 189

我可以手动读取 cookie 行,例如:(Set-Cookie: AuthCode=ABC...我当然可以编写脚本)。所以我可以AuthCode用于后续请求。

我正在尝试在 R 中使用 RCurl 和/或 httr 做同样的事情(仍然不知道哪个更适合我的任务)。

当我尝试:

library(httr)

POST("http://www.example.com/login",
     body= list(RURL="http=//www.example.com/r",
                user="bob", password="hello"),
     user_agent("Mozilla/5.0"))  

我收到类似这样的回复:

Response [http://www.example.com/error]
  Status: 411
  Content-type: text/html
<h1>Length Required</h1> 

总的来说,我知道 411 错误,我可以尝试修复该请求;但我没有在 cURL 中得到它,所以我对 POST 命令做错了。

你能帮我把我的 cURL 命令翻译成 RCurl 和/或 httr 吗?

4

3 回答 3

2

httr automatically preserves cookies across calls to the same site, as illustrated by these two calls to http://httpbin.org

GET("http://httpbin.org/cookies/set?a=1")
# Response [http://httpbin.org/cookies]
#   Status: 200
#   Content-type: application/json
# {
#    "cookies": {
#     "a": "1"
#   }
# } 

GET("http://httpbin.org/cookies")
# Response [http://httpbin.org/cookies]
#   Status: 200
#   Content-type: application/json
# {
#   "cookies": {
#     "a": "1"
#   }
# } 

Perhaps the problem is that you're sending your data as application/x-www-form-urlencoded, but the default in httr is multipart/form-data, so use multipart = FALSE in your POST call.

于 2013-02-21T15:02:36.283 回答
2

根据 Juba 的建议,这是一个有效的 RCurl 模板。

该代码模拟浏览器行为,因为它:

  1. 在登录屏幕上检索 cookie 和
  2. 在包含实际数据的以下页面请求中重用它们。


### RCurl login and browse private pages ###

library("RCurl")

loginurl ="http=//www.*****"
mainurl  ="http=//www.*****"
agent    ="Mozilla/5.0"

#User account data and other login pars
pars=list(
     RURL="http=//www.*****",
     Username="*****",
     Password="*****"
)

#RCurl pars     
curl = getCurlHandle()
curlSetOpt(cookiejar="cookiesk.txt",  useragent = agent, followlocation = TRUE, curl=curl)
#or simply
#curlSetOpt(cookiejar="", useragent = agent, followlocation = TRUE, curl=curl)

#post login form
web=postForm(loginurl, .params = pars, curl=curl)

#go to main url with real data
web=getURL(mainurl, curl=curl)

#parse/print content of web
#..... etc. etc.


#This has the side effect of saving cookie data to the cookiejar file 
rm(curl)
gc()
于 2013-02-27T22:49:16.803 回答
1

这是一种创建发布请求、保留和重用生成的 cookie 的RCurl方法,例如在需要身份验证时获取网页:

library(RCurl)
curl <- getCurlHandle()
curlSetOpt(cookiejar="/tmp/cookies.txt", curl=curl)
postForm("http://example.com/login", login="mylogin", passwd="mypasswd", curl=curl)
getURL("http://example.com/anotherpage", curl=curl)
于 2013-02-21T11:19:14.243 回答