Zillow api with R problem - XML

I am trying to read information from the Zillow API and am encountering some data structure problems in R. My outputs should be xml and appear, but do not behave like xml.

In particular, the object returned to me by GetSearchResults () has a format similar to XML, but not quite correct for reading in R readings of XML.

Can you tell me how I should approach this?

#set directory
setwd('[YOUR DIRECTORY]')

# setup libraries
library(dplyr)
library(XML)
library(ZillowR)
library(RCurl)

# setup api key
set_zillow_web_service_id('[YOUR API KEY]')

xml = GetSearchResults(address = '120 East 7th Street', citystatezip = '10009')
data = xmlParse(xml)

This causes the following error:

Error: XML content does not seem to be XML

The Zillow API documentation clearly states that the output must be XML, and it certainly does. I would like to be able to easily access the various components of the API output for larger data manipulation / aggregation. Let me know if you have any ideas.

+4
3

Zillow API. , XML R-, , . , , . getValRange Zestimate.

results <- xmlToList(xml$response[["results"]])

getValRange <- function(x, hilo) {
  ifelse(hilo %in% unlist(dimnames(x)), x["text",hilo][[1]], NA)
}

out <- apply(results, MAR=2, function(property) {
  zpid <- property$zpid
  links <- unlist(property$links)
  address <- unlist(property$address)
  z <- property$zestimate
  zestdf <- list(
    amount=ifelse("text" %in% names(z$amount), z$amount$text, NA),
    lastupdated=z$"last-updated",
    valueChange=ifelse(length(z$valueChange)==0, NA, z$valueChange),
    valueLow=getValRange(z$valuationRange, "low"),
    valueHigh=getValRange(z$valuationRange, "high"),
    percentile=z$percentile)  
  list(id=zpid, links, address, zestdf)
})

data <- as.data.frame(do.call(rbind, lapply(out, unlist)), 
  row.names=seq_len(length(out)))

:

> data[,c("id", "street", "zipcode", "amount")]
          id              street zipcode  amount
1 2098001736 120 E 7th St APT 5A   10009 2321224
2 2101731413 120 E 7th St APT 1B   10009 2548390
3 2131798322 120 E 7th St APT 5B   10009 2408860
4 2126480070 120 E 7th St APT 1A   10009 2643454
5 2125360245 120 E 7th St APT 2A   10009 1257602
6 2118428451 120 E 7th St APT 4A   10009    <NA>
7 2125491284 120 E 7th St FRNT 1   10009    <NA>
8 2126626856 120 E 7th St APT 2B   10009 2520587
9 2131542942 120 E 7th St APT 4B   10009 1257676
+4
# setup libraries
pacman::p_load(dplyr,XML,ZillowR,RCurl) # I use pacman, you don't have to

# setup api key
set_zillow_web_service_id('X1-mykey_31kck')

xml <- GetSearchResults(address = '120 East 7th Street', citystatezip = '10009')
dat <- unlist(xml)
str(dat)

chr [1: 653] "120 East 7th Street" "10009" " " "0" "" "" "" "zpid" "" "2131798322" ""...  - attr (*, "names" ) = chr [1: 653] "request.address" "request.citystatezip" "message.text" "message.code"...

dat <- as.data.frame(dat)
dat <- gsub("text","", dat$dat)

, , , :

head(dat, 20)
 [1] "120 East 7th Street"                                                                     
 [2] "10009"                                                                                   
 [3] "Request successfully processed"                                                          
 [4] "0"                                                                                       
 [5] "response"                                                                                
 [6] "results"                                                                                 
 [7] "result"                                                                                  
 [8] "zpid"                                                                                    
 [9] ""                                                                                        
[10] "2131798322"                                                                              
[11] "links"                                                                                   
[12] "homedetails"                                                                             
[13] ""                                                                                        
[14] "http://www.zillow.com/homedetails/120-E-7th-St-APT-5B-New-York-NY-10009/2131798322_zpid/"
[15] "mapthishome"                                                                             
[16] ""                                                                                        
[17] "http://www.zillow.com/homes/2131798322_zpid/"                                            
[18] "comparables"                                                                             
[19] ""                                                                                        
[20] "http://www.zillow.com/homes/comps/2131798322_zpid/"
+2

, , API ( XML). , .

R, . github - https://github.com/billzichos/homer. .

, Zillow 36086728, .

home_estimate("36086728")
0
source

Source: https://habr.com/ru/post/1650473/


All Articles