-
Notifications
You must be signed in to change notification settings - Fork 1
/
GET.R
executable file
·70 lines (50 loc) · 1.56 KB
/
GET.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
library(httr)
library(jsonlite)
library(magrittr)
vader <- GET("http://swapi.co/api/people/?search=vader")
vader2 <- GET("http://swapi.co/api/people/", query = list(search = "vader"))
str(vader)
listviewer::jsonedit(vader)
vader$content
vader$status_code
vader$headers$`content-type`
names(vader)
text_content <- content(vader, as = "text", encoding = "UTF-8")
text_content
listviewer::jsonedit(text_content)
parsed_content <- content(vader, as = "parsed")
names(parsed_content)
parsed_content$count
str(parsed_content$results)
parsed_content$results[[1]]$name
parsed_content$results[[1]]$terrain
json_content <- text_content %>% fromJSON
json_content
vader_data <- json_content$results
names(vader_data)
vader_data$name
vader_data$terrain
json_parse <- function(req) {
text <- content(req, as = "text", encoding = "UTF-8")
if (identical(text, "")) warn("Нема чого парсить")
fromJSON(text)
}
planets <- GET("http://swapi.co/api/planets") %>% stop_for_status() %>% json_parse()
names(planets)
planets$count
length(planets$results$name)
planets$`next`
next_page <- GET(planets$`next`) %>% stop_for_status() %>% json_parse()
next_page$results$name
planets <- GET("http://swapi.co/api/planets") %>% stop_for_status() %>% json_parse()
next_page <- planets$`next`
planets <- planets$results
while(!is.null(next_page)) {
more_planets <- GET(next_page) %>% stop_for_status() %>% json_parse()
planets <- rbind(planets, more_planets$results)
next_page <- more_planets$`next`
}
length(planets$name)
planets$name
head(planets)
xlsx::write.xlsx(planets,"planets.xlsx")