【问题标题】:Scrape password-protected website in R在 R 中抓取受密码保护的网站
【发布时间】:2014-07-13 14:16:04
【问题描述】:

我正在尝试从 R 中受密码保护的网站中抓取数据。四处阅读,似乎 httr 和 RCurl 包是使用密码身份验证进行抓取的最佳选择(我还研究了 XML 包) .

我要抓取的网站如下(您需要一个免费帐户才能访问完整页面): http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2

这是我的两次尝试(用我的用户名替换“用户名”,用我的密码替换“密码”):

#This returns "Status: 200" without the data from the page:
library(httr)
GET("http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2", authenticate("username", "password"))

#This returns the non-password protected preview (i.e., not the full page):
library(XML)
library(RCurl)
readHTMLTable(getURL("http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2", userpwd = "username:password"))

我查看了其他相关帖子(以下链接),但不知道如何将他们的答案应用于我的案例。

How to use R to download a zipped file from a SSL page that requires cookies

How to webscrape secured pages in R (https links) (using readHTMLTable from XML package)?

Reading information from a password protected site

R - RCurl scrape data from a password-protected site

http://www.inside-r.org/questions/how-scrape-data-password-protected-https-website-using-r-hold

【问题讨论】:

    标签: xml r web-scraping rcurl httr


    【解决方案1】:

    您可以使用 RSelenium。我使用的是开发版本,因为您可以在没有 Selenium 服务器的情况下运行 phantomjs

    # Install RSelenium if required. You will need phantomjs in your path or follow instructions
    # in package vignettes
    # devtools::install_github("ropensci/RSelenium")
    # login first
    appURL <- 'http://subscribers.footballguys.com/amember/login.php'
    library(RSelenium)
    pJS <- phantom() # start phantomjs
    remDr <- remoteDriver(browserName = "phantomjs")
    remDr$open()
    remDr$navigate(appURL)
    remDr$findElement("id", "login")$sendKeysToElement(list("myusername"))
    remDr$findElement("id", "pass")$sendKeysToElement(list("mypass"))
    remDr$findElement("css", ".am-login-form input[type='submit']")$clickElement()
    
    appURL <- 'http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2'
    remDr$navigate(appURL)
    tableElem<- remDr$findElement("css", "table.datamedium")
    res <- readHTMLTable(header = TRUE, tableElem$getElementAttribute("outerHTML")[[1]])
    > res[[1]][1:5, ]
    Rank             Name Tm/Bye Age Exp Cmp Att  Cm%  PYd Y/Att PTD Int Rsh  Yd TD FantPt
    1    1   Peyton Manning  DEN/4  38  17 415 620 66.9 4929  7.95  43  12  24   7  0 407.15
    2    2       Drew Brees   NO/6  35  14 404 615 65.7 4859  7.90  37  16  22  44  1 385.35
    3    3    Aaron Rodgers   GB/9  31  10 364 560 65.0 4446  7.94  33  13  52 224  3 381.70
    4    4      Andrew Luck IND/10  25   3 366 610 60.0 4423  7.25  27  13  62 338  2 361.95
    5    5 Matthew Stafford  DET/9  26   6 377 643 58.6 4668  7.26  32  19  34 102  1 358.60
    

    最后当你完成关闭phantomjs

    pJS$stop()
    

    如果您想使用传统浏览器,例如 firefox(如果您想坚持使用 CRAN 上的版本),您可以使用:

    RSelenium::startServer()
    remDr <- remoteDriver()
    ........
    ........
    remDr$closeServer()
    

    代替相关的phantomjs 调用。

    【讨论】:

    • 谢谢,这是一种非常通用的方法来解决这个问题。
    • 虽然总体上这是一个非常有用的答案,但可以注意到最近该软件包有所改进,允许在不需要 phantomjs 的情况下更方便地浏览 chrome、firefox 或 IE,例如,使用rD &lt;- RSelenium::rsDriver(port = 5555L, 'firefox'); remDr &lt;- rD[["client"]] 之后按照原始答案进行操作。
    • @Nutle 的优点和 phantom 函数已被弃用,有利于 wdman::phantomjs 所以也许这个答案需要更新
    【解决方案2】:

    我没有用于测试的帐户,但也许这会起作用:

    library(httr)
    library(XML)
    
    handle <- handle("http://subscribers.footballguys.com") 
    path   <- "amember/login.php"
    
    # fields found in the login form.
    login <- list(
      amember_login = "username"
     ,amember_pass  = "password"
     ,amember_redirect_url = 
       "http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2"
    )
    
    response <- POST(handle = handle, path = path, body = login)
    

    现在,响应对象可能包含您需要的内容(或者您可以在登录请求后直接查询感兴趣的页面;我不确定重定向是否有效,但它是 Web 表单中的一个字段),并且handle 可能会重新用于后续请求。无法测试;但这在很多情况下都适用于我。

    您可以使用XML输出表格

    > readHTMLTable(content(response))[[1]][1:5,]
      Rank             Name Tm/Bye Age Exp Cmp Att  Cm%  PYd Y/Att PTD Int Rsh  Yd TD FantPt
    1    1   Peyton Manning  DEN/4  38  17 415 620 66.9 4929  7.95  43  12  24   7  0 407.15
    2    2       Drew Brees   NO/6  35  14 404 615 65.7 4859  7.90  37  16  22  44  1 385.35
    3    3    Aaron Rodgers   GB/9  31  10 364 560 65.0 4446  7.94  33  13  52 224  3 381.70
    4    4      Andrew Luck IND/10  25   3 366 610 60.0 4423  7.25  27  13  62 338  2 361.95
    5    5 Matthew Stafford  DET/9  26   6 377 643 58.6 4668  7.26  32  19  34 102  1 358.60
    

    【讨论】:

    • 这对我有用。我已经编辑了内容输出
    • 我测试了两个答案,它们都很好用。我选择这个是因为它简单。
    • 也许对于其他网站,RSelenium 可能会派上用场;这些网站并不总是像这个那样直截了当。我要记住 phantomjs。
    • 登录表单的信息是如何找到的?
    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多