2016-02-13 5 views
5

Sto provando a grattare il numero di telefono da questo sito Web: http://olx.pl/oferta/pokoj-1-os-bielany-encyklopedyczna-CID3-IDdX6wf.html#c1c0e14c53. Il numero di telefono può essere grattata con rvest pacchetto con selettore .\'id_raw\'\::nth-child(1) span+ div strong (suggerito da selectorGadget.Come eseguire il web-scrap con informazioni su clic con R?

Il problema è che le informazioni possono essere ottenute dopo la sua maschera viene cliccato. Così in qualche modo devo aprire una sessione, fornire un click e poi raschiare informazioni.

EDIT Tra l'altro non è un imho collegamento. Date un'occhiata alla fonte. ho un problema, perché io sono un utente normale R, non un programmatore javascript.

enter image description here

+1

Hai provato RSelenium? – Jota

risposta

6

si può afferrare i dati incorporati nei tag <li> che racconta il gestore onclick cosa fare e solo ottenere i dati direttamente:

library(httr) 
library(rvest) 
library(purrr) 
library(stringr) 

URL <- "http://olx.pl/oferta/pokoj-1-os-bielany-encyklopedyczna-CID3-IDdX6wf.html#c1c0e14c53" 

pg <- read_html(URL) 

html_nodes(pg, "li.rel") %>%  # get the 'special' <li> tags 
    html_attrs() %>%     # extract all the attrs (they're non-standard) 
    flatten_chr() %>%    # list to character vector 
    keep(~grepl("rel \\{", .x)) %>% # only want ones with 'hidden' secret data 
    str_extract("(\\{.*\\})") %>% # only get the data 
    unique() %>%      # there are duplicates 
    map_df(function(x) { 

    path <- str_match(x, "'path':'([[:alnum:]]+)'")[,2]     # extract out the path 
    id <- str_match(x, "'id':'([[:alnum:]]+)'")[,2]      # extract out the id 

    ajax <- sprintf("http://olx.pl/ajax/misc/contact/%s/%s/", path, id) # make the AJAX/XHR URL 
    value <- content(GET(ajax))$value         # get the data 

    data.frame(path=path, id=id, value=value, stringsAsFactors=FALSE) # make a data frame 

    }) 

## Source: local data frame [3 x 3] 
## 
##   path id  value 
##   (chr) (chr)  (chr) 
## 1  phone dX6wf 503 155 744 
## 2  skype dX6wf e.bobruk 
## 3 communicator dX6wf  7686136 

Dopo aver fatto tutto questo, sono abbastanza deluso che il sito non ha migliori termini di servizio/utilizzo. È abbastanza ovvio che in realtà non vogliono che tu scriva questi dati.

+0

Bello che ci sia una soluzione senza dover utilizzare software/programmi esterni. Ti sei mai imbattuto in una situazione in cui hai ** bisogno ** di usare qualcosa come "selenio", o puoi fare di solito tutto in "R"? – tospig

+1

Provo a non usarlo poiché gli idiomi presentati nel pacchetto RSelenium utilizzano un IMO di "Hadleyverse". Ma sicuramente ci sono momenti in cui è necessario. – hrbrmstr

3

Ecco una soluzione che utilizza RSelenium, (RSelenium introduction) e phantomjs.

Tuttavia, io non sono sicuro di come utilizzabile è perché corre molto lento sulla mia macchina, e io non sono un esperto di phantomjs o selenio in modo da non so dove i miglioramenti di velocità possono essere ancora fatte, in modo qualcosa di esaminare ...

Modifica

ho provato di nuovo questo e sembra essere ok per la velocità.

library(RSelenium) 
library(rvest) 

## Terminal command to start selenium (on ubuntu) 
## cd ~/selenium && java -jar selenium-server-standalone-2.48.2.jar 
url <- "http://olx.pl/oferta/pokoj-1-os-bielany-encyklopedyczna-CID3-IDdX6wf.html#c1c0e14c53" 

RSelenium::startServer() 
remDr <- remoteDriver(browserName = "phantomjs") 

remDr$open() 
remDr$navigate(url) 

# css <- ".cpointer:nth-child(1)" ## couldn't get this to work 
xp <- "//div[@class='contactbox-indent rel brkword']" 
webElem <- remDr$findElement(using = 'xpath', xp) 

# webElem <- remDr$findElement(using = 'css selector', css) 
webElem$clickElement() 

## the page source now includes the clicked element 
page_source <- remDr$getPageSource()[[1]] 
pos <- regexpr('class=\\"xx-large', page_source) 

## you could write a more intelligent regex, but this works for now 
phone_number <- substr(page_source, pos + 11, pos + 21) 
phone_number 
# "503 155 744" 

# remDr$close() 
# remDr$closeServer() 
Problemi correlati