2014-07-13 9 views
10

Sto provando a raschiare dati da un sito Web protetto da password in R. Leggendo intorno, sembra che i pacchetti httr e RCurl siano le migliori opzioni per raschiare con l'autenticazione tramite password (Ho anche esaminato il pacchetto XML).Scrape sito Web protetto da password in R

Il sito che sto cercando di raschiare è al di sotto (è necessario un account gratuito per poter accedere alla pagina intera): http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2

Qui sono i miei due tentativi (sostituendo "username" con il mio nome utente e la " password" con la password):

#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")) 

ho guardato in altri posti rilevanti (link sotto), ma non riesco a capire come applicare le loro risposte al mio caso.

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

risposta

11

Non ho un account per controllare, ma forse questo funzionerà:

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) 

Ora, l'oggetto risposta potrebbe contenere ciò che è necessario (o forse è possibile interrogare direttamente la pagina di interesse dopo la richiesta di accesso, non sono sicuro che il reindirizzamento funzionerà, ma è un campo nel modulo Web) e handle potrebbe essere riutilizzato per le richieste successive. Non posso testarlo; ma questo funziona per me in molte situazioni.

È possibile emettere la tabella utilizzando 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 
+1

Questo funziona per me. Ho modificato con l'output di contenuto – jdharrison

+0

Cool! Non penso che sia molto più facile ... – Stefan

+0

Ho testato entrambe le risposte e funzionano entrambe alla grande.Ho selezionato questo per la sua semplicità. – dadrivr

11

È possibile utilizzare RSelenium. Ho usato la versione di sviluppo come è possibile eseguire phantomjs senza un server 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 

Infine, quando si è finito vicino phantomjs

pJS$stop() 

Se si desidera utilizzare un browser tradizionale come firefox per esempio (se si voleva aderire alla versione on CRAN) si usa:

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

al posto delle relative chiamate phantomjs.

+0

Grazie, questo è un approccio molto versatile per risolvere questo. –

+1

Mentre nel complesso questa è una risposta molto utile, si può notare che ultimamente il pacchetto ha avanzato un po ', consentendo una navigazione più comoda attraverso chrome, firefox o IE senza la necessità di phantomjs, ad esempio, usando '' rD <- RSelenium: : rsDriver (porta = 5555L, 'firefox'); remDr <- rD [["client"]] '' e seguendo la risposta originale in seguito. – Nutle

+1

@Nutle buoni punti e la funzione phantom è deprecata a favore di wdman :: phantomjs quindi forse questa risposta necessita di aggiornamento – jdharrison