xml
  • r
  • xpath
  • web-scraping
  • 2013-08-13 7 views 5 likes 
    5

    Ich habe einen HTML- und einen R-Code wie diese und muss jeden Knotenwert auf seine Eltern-ID in einem dat.frame beziehen. Es gibt verschiedene Informationen für jede Person.R: Wie erhalten Elternattribute und Knotenwerte zur Ortszeit?

    example <- "<div class='person' id='1'> 
    <div class='phone'>555-5555</div> 
    <div class='email'>[email protected]</div> 
    </div> 
    <div class='person' id='2'> 
    <div class='phone'>123-4567</div> 
    <div class='email'>[email protected]</div> 
    </div> 
    <div class='person' id='3'> 
    <div class='phone'>987-6543</div> 
    <div class='age'>32</div> 
    <div class='city'>New York</div> 
    </div>" 
    
    doc = htmlTreeParse(example, useInternalNodes = T) 
    
    values <- xpathSApply(doc, "//*[@class='person']/div", xmlValue) 
    variables <- xpathSApply(doc, "//*[@class='person']/div", xmlGetAttr, 'class') 
    id <- xpathSApply(doc, "//*[@class='person']", xmlGetAttr, 'id') 
    
    # The problem: create a data.frame(id,variables,values) 
    

    Mit xpathSApply(), kann ich Telefon, E-Mail und Alte Werte sowie Person Attribute (id) erhalten. Diese Informationen werden jedoch isoliert angezeigt, und ich muss sie auf die richtige dat.frame-Variable und die richtige Person verweisen. In meinen realen Daten gibt es viele verschiedene Informationen, so dass dieser Prozess der Benennung jeder Variable automatisch sein muss.

    Mein Ziel ist es, ein data.frame wie dieses zu erstellen, das jede ID mit ihren richtigen Daten in Verbindung bringt.

    id variables   values 
    1 1  phone  555-5555 
    2 1  email [email protected] 
    3 2  phone  123-4567 
    4 2  email [email protected] 
    5 3  phone  987-6543 
    6 3  age    32 
    7 3  city  New York 
    

    Ich glaube, ich würde eine Funktion erstellen, muß innerhalb xpathSApply zu verwenden, die die Person, Telefon und die Person id zur gleichen Zeit bekommen würde, so würden sie in Beziehung gesetzt werden, aber ich habe keinen Erfolg gehabt mit dass bis jetzt.

    Kann mir jemand helfen?

    Antwort

    7

    Im Allgemeinen sein Gehen nicht einfach sein:

    idNodes <- getNodeSet(doc, "//div[@id]") 
    ids <- lapply(idNodes, function(x) xmlAttrs(x)['id']) 
    values <- lapply(idNodes, xpathApply, path = './div[@class]', xmlValue) 
    attributes <- lapply(idNodes, xpathApply, path = './div[@class]', xmlAttrs) 
    do.call(rbind.data.frame, mapply(cbind, ids, values, attributes)) 
        V1    V2 V3 
    1 1  555-5555 phone 
    2 1 [email protected] email 
    3 2  123-4567 phone 
    4 2 [email protected] email 
    5 3  987-6543 phone 
    6 3    32 age 
    7 3  New York city 
    

    Die oben geben Sie Attribut-Wert-Paare assumming sie in einem div mit einer zugehörigen id verschachtelt sind.

    UPDATE: , wenn Sie es in einem xpathApply Typ Aufruf wickeln wollen

    utilFun <- function(x){ 
        id <- xmlGetAttr(x, 'id') 
        values <- sapply(xmlChildren(x, omitNodeTypes = "XMLInternalTextNode"), xmlValue) 
        attributes <- sapply(xmlChildren(x, omitNodeTypes = "XMLInternalTextNode"), xmlAttrs) 
        data.frame(id = id, attributes = attributes, values = values, stringsAsFactors = FALSE) 
    } 
    res <- xpathApply(doc, '//div[@id]', utilFun) 
    do.call(rbind, res) 
        id attributes   values 
    1 1  phone  555-5555 
    2 1  email [email protected] 
    3 2  phone  123-4567 
    4 2  email [email protected] 
    5 3  phone  987-6543 
    6 3  age    32 
    7 3  city  New York 
    
    Verwandte Themen