我正在尝试从https://www.booking.com/country.html抓取数据。
这个想法是提取有关为特定国家列出的任何类型的住宿的所有数字。
输出需要在 Excel 文件的“A 列”中列出所有国家/地区的列表,以及每个国家/地区与国家名称相邻的不同物业类型(例如公寓、旅馆、度假村等)的相关列表数量在单独的列中。
我需要捕获给定国家/地区所有属性类型的所有详细信息。
上图描述了 Excel 中所需的输出格式。我可以使用以下代码获取国家/地区,但不能使用属性类型及其各自的数据。
如何以迭代方式获取所有国家/地区的数据并写入 csv。
library(rvest)
library(reshape2)
library(stringr)
url <- "https://www.booking.com/country.html"
bookingdata <- read_html(url)
#extracting the country
country <- html_nodes(bookingdata, "h2 > a") %>%
html_text()
write.csv(country, 'D:\\web scraping\\country.csv' ,row.names = FALSE)
print(country)
#extracting the data inside the inner div
html_nodes(bookingdata, "div >div > div > ul > li > a")%>%
html_text()
for (i in country) {
print(i)
html_nodes(pg, "ul > li > a") %>%
html_text()
print(accomodation)
}
#getting all the data
accomodation <- html_nodes(pg, "ul > li > a") %>%
html_text()
#separating the numbers
accomodation.num <- (str_extract(accomodation, "[0-9]+"))
#separating the characters
accomodation.char <- (str_extract(accomodation,"[aA-zZ]+"))
#separating unique characters
unique(accomodation.char)