-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrip_search.R
49 lines (35 loc) · 911 Bytes
/
trip_search.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
trip_searchR <- function(search_term, n = 1000){
require(rvest)
require(tidyverse)
search_url <- url <- paste0("https://www.tripdatabase.com/search/xml?key=PHE31413&criteria=", search_term, "&max=", n)
search <- read_xml(url)
## Extract IDs
ids <- search %>%
xml_contents() %>%
xml_nodes("id") %>%
as_list() %>%
unlist()
titles <- search %>%
xml_contents() %>%
xml_nodes("title") %>%
as_list() %>%
unlist() %>%
cbind(ids)
links <- search %>%
xml_contents() %>%
xml_nodes("link") %>%
as_list() %>%
unlist() %>%
cbind(titles)
date <- search %>%
xml_contents() %>%
xml_nodes("pubDate") %>%
as_list() %>%
unlist() %>%
cbind(links)
result <- data.frame(date)
colnames(result) <- c("date", "link", "title", "id")
result <- result %>%
select(id, date, title, link) %>%
mutate_if(is.factor, as.character)
}