Ho preso il seguente codice dal pacchetto rNomads e l'ho modificato un po '.R WebCrawler - Il contenuto XML non sembra essere XML:
Quando inizialmente eseguirlo ottengo:
> WebCrawler(url = "www.bikeforums.net")
[1] "www.bikeforums.net"
[1] "www.bikeforums.net"
Warning message:
XML content does not seem to be XML: 'www.bikeforums.net'
Ecco il codice:
require("XML")
# cleaning workspace
rm(list = ls())
# This function recursively searches for links in the given url and follows every single link.
# It returns a list of the final (dead end) URLs.
# depth - How many links to return. This avoids having to recursively scan hundreds of links. Defaults to NULL, which returns everything.
WebCrawler <- function(url, depth = NULL, verbose = TRUE) {
doc <- XML::htmlParse(url)
links <- XML::xpathSApply(doc, "//a/@href")
XML::free(doc)
if(is.null(links)) {
if(verbose) {
print(url)
}
return(url)
} else {
urls.out <- vector("list", length = length(links))
for(link in links) {
if(!is.null(depth)) {
if(length(unlist(urls.out)) >= depth) {
break
}
}
urls.out[[link]] <- WebCrawler(link, depth = depth, verbose = verbose)
}
return(urls.out)
}
}
# Execution
WebCrawler(url = "www.bikeforums.net")
Qualsiasi raccomandazione che cosa sto facendo male?
UPDATE
Ciao ragazzi,
ho iniziato questa abbondanza, perché penso che nella comunità R c'è bisogno di una tale funzione, che può eseguire la scansione pagine web. La soluzione, che avrebbe vinto la bontà dovrebbe mostrare una funzione che prende due parametri:
WebCrawler(url = "www.bikeforums.net", xpath = "\\title")
- Come uscita desidero avere un frame di dati con due colonne: il link al sito e se l'espressione esempio xpath corrisponde una colonna con l'espressione corrispondente.
Apprezzo molto il vostro risposte
Se si esegue 'doc <- XML :: htmlParse (" http://www.bikeforums.net ")); links <- XML :: xpathSApply (doc, "// a/@ href") 'manualmente funziona ma all'interno della funzione (usando la modalità di debug) doc torna vuoto ... questo è strano – Rentrop
Prova con' url = "http : //www.bikeforums.net "' e fammi sapere –
@dimitris_ps Quando eseguo '> WebCrawler (url =" http://www.bikeforums.net ")' Ottengo 'Errore: impossibile caricare l'entità esterna "/"' – mrquad