2016-07-15 3 views
0

ISBN을 기반으로 API (http://www.knigoed.info/api-prices.html)에서 도서 가격 데이터를 얻으려고합니다.JSON API가있는 R 루프 소스

아이디어는

isbns<- c("9785170922789", "9785170804801", "9785699834174", "9785699717255", "9785170869237") 


getISBNprice <- function(ISBN, source="http://www.knigoed.info/api/Prices?code=") { 
     pathA <- source 
     for (i in 1:length(ISBN)) { 

       ISB <- ISBN[i] 
       AAA <- paste(pathA, ISB, "&sortPrice=DESC&country=RU", sep="") 
       document <- fromJSON(AAA, flatten = FALSE) 

       dfp <- document$prices 
       dfp <- cbind(dfp,ISB) 
       # dfp <- cbind(dfp,BookID=document$bookId) 
       # dfp <- cbind(dfp,Title=document$title) 
       # dfp <- cbind(dfp,Author=document$author) 
       # dfp <- cbind(dfp,Publisher=document$publisher) 
       # dfp <- cbind(dfp,Series=document$series) 
       # dfp <- cbind(dfp,Picture=document$picture) 

       if (!exists("AAAA")) {AAAA<- dfp} else {bind_rows(AAAA, dfp) } 
     } 

     AAAA 
}   

그러나 함수가 오류를 반환합니다 (다른 공급 업체의 가격 또는 적어도 Data.Frame) 데이터 사용 가능한 모든 정보와 프레임을 얻을 수있는 기능에 ISBN이의 벡터를 제출하는 것입니다 : 1. bind_rows_ (x, .id)에서 같지 않은 요소 수준 : 문자로 강제 변환 됨 2 : bind_rows_ (x, .id)에서 : 동일하지 않은 요소 수준 : 문자로 강제 변환 됨 3 : bind_rows_ (x, .id) : 동일하지 않은 요소 수준 : 문자로 강제 변환 4 : bind_rows_ (x, .id)에서 : 같지 않은 요소 수준 : 문자로 강제 변환

답변

1

가장 쉬운 방법은 make a list from the start입니다. 나중에 쉽게 단순화 할 수 있습니다. purrr 패키지를 사용하면 훨씬 쉽게 작업 할 수 있지만 원하는 경우 여기의 사용법은 기본 lapplymapply/Map으로 바꿀 수 있습니다.

library(purrr) 

# Paste is vectorized, so make a list of URLs all at once. 
# `httr` can make a URL out of a list of named parameters, if it's more convenient. 
results <- paste0("http://www.knigoed.info/api/Prices?code=", 
        isbns, 
        "&sortPrice=DESC&country=RU") %>% 
    # Iterate over vector of URLs, using fromJSON to pull and parse the request. 
    # map, like lapply, will put the results into a list. 
    map(jsonlite::fromJSON, flatten = FALSE) 

      # Grab "prices" element of each top-level list element 
results %>% map('prices') %>% 
    # Iterate in parallel (like mapply/Map) over prices and isbns, making a data.frame of 
    # each. map2_df will coerce the resulting list of data.frames to a single data.frame. 
    map2_df(isbns, ~data.frame(isbn = .y, .x, stringsAsFactors = FALSE)) %>% 
    # For pretty printing 
    tibble::as_data_frame() 

## # A tibble: 36 x 10 
##    isbn shopId  name  domain 
##   <chr> <chr>  <chr>  <chr> 
## 1 9785170922789  29 Магистр booka.ru 
## 2 9785170922789  3 Лабиринт labirint.ru 
## 3 9785170922789  20 LitRes.ru litres.ru 
## 4 9785170804801  29 Магистр booka.ru 
## 5 9785170804801  2 Read.ru  read.ru 
## 6 9785170804801  3 Лабиринт labirint.ru 
## 7 9785170804801  63  Эксмо eksmo.ru 
## 8 9785170804801  1 OZON.ru  ozon.ru 
## 9 9785170804801  4 My-shop.ru my-shop.ru 
## 10 9785170804801  1 OZON.ru  ozon.ru 
## # ... with 26 more rows, and 6 more variables: url <chr>, available <lgl>, downloadable <lgl>, 
## # priceValue <dbl>, priceSuffix <chr>, year <int> 
+0

매우 명확하고 자세한 설명과 함께 완벽한 솔루션을 제공해 주셔서 감사합니다. 털이 많은 것을 배울 수 있도록 최선을 다하겠습니다. – Krank