Я хочу загрузить все файлы с именами 'lists.csv' и 'reviews.csv' для всей страны из "http://insideairbnb.com/get-the-data.html"? Я могу сделать это, написав каждую ссылку, но возможно ли сделать это в al oop для обоих файлов?
Затем мне нужно будет соединить их с SQL и проанализировать их в r.
rm(list=ls())
# We need rvest and httr packages to crawl and download the files
library(rvest)
library(httr)
# We need xmlToList() from package XML
library(XML)
# We need bind_rows() in package "dplyr"
library(dplyr)
library(purrr)
page <- read_html("http://insideairbnb.com/get-the-data.html")
# Get all hrefs (i.e. all links present on the website)
links <- page %>%
html_nodes("a") %>%
html_attr("href")
wanted <- grep('listings.csv.gz', links)
wanted.links <- links[wanted]
wanted.cols = c("host_is_superhost", "summary", "host_identity_verified", "street",
"city", "property_type", "room_type", "bathrooms",
"bedrooms", "beds", "price", "security_deposit", "cleaning_fee",
"guests_included", "number_of_reviews", "instant_bookable",
"host_response_rate", "host_neighbourhood",
"review_scores_rating", "review_scores_accuracy","review_scores_cleanliness",
"review_scores_checkin" ,"review_scores_communication",
"review_scores_location", "review_scores_value", "space",
"description", "host_id", "state", "latitude", "longitude")
read.gz.url <- function(link) {
con <- gzcon(url(link))
df <- read.csv(textConnection(readLines(con)))
close(con)
df <- df %>% select(wanted.cols) %>%
mutate(source.url = link)
df
}
all.df = list()
for (i in seq_along(wanted.links)) {
all.df[[i]] = read.gz.url(wanted.links[i])
}
all.df = map(all.df, as_tibble)