Das Problem ist, ich bin mir sicher, ist ziemlich einfach. Aber ich kann nicht herausfinden, wie es funktioniert. Ich habe vier Websites wie so:Erstellen Sie eine Liste von html() Elementen
require(xml2)
require(rvest)
html1 <- html("http://academic.research.microsoft.com/RankList?entitytype=4&topdomainid=2&subdomainid=6&last=0&orderby=6")
html2 <- html("http://academic.research.microsoft.com/RankList?entitytype=3&topdomainid=2&subdomainid=6&last=0&orderby=6")
html3 <- html("http://academic.research.microsoft.com/RankList?entitytype=3&topdomainid=2&subdomainid=7&last=0&orderby=6")
html4 <- html("http://academic.research.microsoft.com/RankList?entitytype=4&topDomainID=2&subDomainID=7&last=0&orderby=6")
htmlPages <- c(html1,html2,html3,html4)
Ich versuche, sie alle innerhalb einer Liste für den einfachen Zugriff innerhalb einer for-Schleife oder etwas zu platzieren. Platzieren Sie sie in der Liste ist kein Problem. Das Problem besteht darin, später darauf zuzugreifen. Was ich damit meine ist, dass ich den Text von den Knoten abholen muss.
getCSSElementText <- function(htmlpage, CSSElement)
{
#Return a vector of the text values of the CSS element the function is looking for
cssNodes <- html_nodes(htmlpage, CSSElement)
cssValues <- html_text(cssNodes)
return(cssValues)
}
Wie nenne ich
getCSSElementText(htmlPages[1], #properCSSTag#)
ich diesen Fehler:
Fehler in UseMethod ("xml_find_all"): keine anwendbare Methode für 'xml_find_all' angewendet ein Objekt der Klasse "Liste"
Hier ist mein ganzer Code nur für den Fall etwas schief gelaufen ist woanders:
library(rvest)
library(xml2)
html1 <- html("http://academic.research.microsoft.com/RankList?entitytype=4&topdomainid=2&subdomainid=6&last=0&orderby=6")
html2 <- html("http://academic.research.microsoft.com/RankList?entitytype=3&topdomainid=2&subdomainid=6&last=0&orderby=6")
html3 <- html("http://academic.research.microsoft.com/RankList?entitytype=3&topdomainid=2&subdomainid=7&last=0&orderby=6")
html4 <- html("http://academic.research.microsoft.com/RankList?entitytype=4&topDomainID=2&subDomainID=7&last=0&orderby=6")
htmlPages <- c(html1,html2,html3,html4)
CSSElementIDs <- c("#ctl00_MainContent_divRankList a", ".staticOrderCol:nth-child(3)", ".staticOrderCol:nth-child(4)")
getCSSElementText <- function(htmlpage, CSSElement)
{
#Return a vector of the text values of the CSS element the function is looking for
cssNodes <- html_nodes(htmlpage, CSSElement)
cssValues <- html_text(cssNodes)
return(cssValues)
}
getCSSElementNumber <- function(htmlpage, CSSElement)
{
#Return a vector of numbers with proper formatting etc from the CSS element the function is looking for
cssNodes <- html_nodes(htmlpage, CSSElement)
cssValues <- html_text(cssNodes)
parsedCssValues <- as.numeric(gsub("\\D", "", cssValues))
return(parsedCssValues)
}
addToDataFrame <- function(df, vector)
{
df[deparse(substitute(vector))] <- vector
return(df)
}
Vielen Dank für Ihre Zeit!
Try 'htmlPages <- Liste (HTML1, html2, html3, html4) 'und dann ' getCSSElementText (htmlPages [[1]], # properCSSTag #) '(zwei eckige Klammern). –
'html()' ist veraltet, verwenden 'read_html' –
Im Allgemeinen ist der einfachste Weg, um mit dieser Situation umzugehen, ein Vektor von URLs zu machen (oder was Sie dazu tun müssen), und mit' lapply' oder 'über sie hinwegzulaufen purrr: map' oder Varianten. Beginnen Sie von Anfang an parallel, nicht auf halbem Weg. – alistaire