在R中search受密码保护的网站

我试图抓取密码保护的网站在R读取数据。看来,httr和RCurl包是用密码authentication(我也看过XML包)的最佳select。

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

这里是我的两个尝试(用我的用户名和密码replace“用户名”和我的密码):

#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 (ie, not the full page): library(XML) library(RCurl) readHTMLTable(getURL("http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2", userpwd = "username:password")) 

我已经看了其他相关的post(下面的链接),但不知道如何将他们的答案应用到我的案件。

如何使用R从需要cookie的SSL页面下载压缩文件

如何在R(https链接)(使用XML包中的readHTMLTable)对web页面进行webscrape?

从密码保护网站读取信息

R – RCurl从密码保护的网站刮取数据

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

我没有一个帐户来testing,但也许这将工作:

 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) 

现在,响应对象可以包含你所需要的东西(或者,也许你可以在login请求后直接查询感兴趣的页面;我不确定redirect是否可用,但它是web表单中的一个字段),并且handle可能是重新用于后续请求。 无法testing; 但是这在很多情况下适用于我。

您可以使用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没有Selenium服务器。

 # 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 

最后当你完成closuresphantomjs

 pJS$stop() 

如果你想使用像Firefox这样的传统浏览器(如果你想坚持在CRAN上的版本),你可以使用:

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

代替相关的phantomjs电话。