2

我想用 R 访问 php 网站
http://centralgreen.com.sg/login.php?login=9-1501&password=mypassword的内容

我在url中传递了一个登录+密码的例子,但是我不知道如何通过url按下登录按钮。

如果可能,我想使用 R 包RCurl

4

2 回答 2

0

表单通过 post 提交 - 从外观上看,您目前正在使用 get 请求,您需要使用 post。

我的猜测是 rcurl 基于 curl - 我知道 curl 可以做到这一点,所以应该是可能的。

于 2011-06-03T07:19:18.797 回答
0

最近我遇到了同样的问题。就我而言,我使用 RCurl 包(带有 POST 请求)解决了这个问题。

在这段代码中,两个请求一个接一个地完成。第一个,是为了获得一个会话cookie(在服务器中启动会话)。我调用的应用程序希望会话在它检查登录凭据时启动(如果您预先发送表单,则不会发生这种情况)。否则会提出一些关于没有 cookie 支持的警告。这可能是提问者的情况(尽管那是很久以前的事了)......或者其他人的情况。

login <- function (xxxx_user, xxxx_pass) {

  url_login <- 'http://centralgreen.com.sg/login.php'

  curlhand <- getCurlHandle()

  curlSetOpt(
     .opts = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")),
      cookiefile = "cookies.txt",
      useragent = 'YOUR R-PACKAGE NAME',
      followlocation = TRUE,
      # might need this in case the server checks for the referer..
      httpheader = "Referer: http://centralgreen.com.sg",
      curl = curlhand)

  # (1) first call to initializate session. you get the session cookie
  getURL(url_login, curl = curlhand)

  params<- list( login = xxxx_user, password = xxxx_pass )
  # might need to add some other hidden form param in case there are..

  # (2) second call, sends the form, along with a session cookie
  html = postForm(url_login, 
    .params = params, 
    curl = curlhand, 
    style="POST")

  # ... perform some grep logic with 'html' to find out weather you are connected 
}

# you call the function...
login("yourusername", "yourpass")

“执行一些 grep 逻辑”注释考虑了这样一个事实,即由于您的目标系统不是为这种编程登录而设计的,它不会给您任何关于尝试结果的好提示......所以您可能需要根据一些关键句子解析您收到的原始 html 字符串(例如:'错误的用户名或密码'......)

希望能帮助到你

于 2013-11-05T18:35:56.170 回答