我试图刮一个网页在booking.com与rvest和问题是,我需要的代码返回NA时,酒店没有评级,例如,所以dataframe将有确切的行数为每个参数我试图刮。
我正在使用的代码,功能完美而不返回NA是这样的:
# Necessary packages
library(rvest)
library(dplyr)
library(httr)
# Base URL of the search results page
base_url <- "https://www.booking.com/searchresults.it.html"
# Parameters we add to the search get the specific results
params <- list(
ss = "Firenze%2C+Toscana%2C+Italia",
efdco = 1,
label = "booking-name-L*Xf2U1sq4*GEkIwcLOALQS267777916051%3Apl%3Ata%3Ap1%3Ap22%2C563%2C000%3Aac%3Aap%3Aneg%3Afi%3Atikwd-65526620%3Alp9069992%3Ali%3Adec%3Adm%3Appccp",
aid = 376363,
lang = "it",
sb = 1,
src_elem = "sb",
src = "index",
dest_id = -117543,
dest_type = "city",
ac_position = 0,
ac_click_type = "b",
ac_langcode = "it",
ac_suggestion_list_length = 5,
search_selected = "true",
search_pageview_id = "2e375b14ad810329",
ac_meta = "GhAyZTM3NWIxNGFkODEwMzI5IAAoATICaXQ6BGZpcmVAAEoAUAA%3D",
checkin = "2023-06-11",
checkout = "2023-06-18",
group_adults = 2,
no_rooms = 1,
group_children = 0,
sb_travel_purpose = "leisure"
)
# Create empty vectors to store the titles, rating, price
titles <- c()
ratings <- c()
prices <- c()
### Loop through each page of the search results
for (page_num in 1:35) {
# Build the URL for the current page
url <- modify_url(base_url, query = c(params, page = page_num))
# Read the HTML of the new page specificated
page <- read_html(url)
# Extract the titles, rating, price from the current page
# Got the elements from Inspect code of the page
titles_page <- page %>% html_elements("div[data-testid='title']") %>% html_text()
prices_page <- titles_page %>% html_element("span[data-testid='price-and-discounted-price']") %>% html_text()
ratings_page <- titles_page %>% html_element("div[aria-label^='Punteggio di']") %>% html_text()
# Append the titles, ratings, prices from the current page to the vector
titles <- c(titles, titles_page)
prices <- c(prices, prices_page)
ratings <- c(ratings, ratings_page)
}
hotel = data.frame(titles, prices, ratings)
print(hotel)```
I have seen being suggested to add a paretn and children node and I have tried this but it does not function:
```titles_page <- page %>% html_elements("div[data-testid='title']") %>% html_text()
prices_page <- titles_page %>% html_element("span[data-testid='price-and-discounted-price']") %>% html_text()
ratings_page <- titles_page %>% html_element("div[aria-label^='Punteggio di']") %>% html_text()```
1条答案
按热度按时间kuhbmx9i1#
titles_page <- page %>% html_elements("div[data-testid='title']") %>% html_text()
正在创建一个字符串向量。不能在下一行代码中解析“titles_page”。
您跳过了创建父节点向量的步骤。回顾您之前的问题/答案How to report NA when scraping a web with R and it does not have value?并查看答案中的
properties <- html_elements(page, xpath=".//div[@data-testid='property-card']")
行。这将返回xml节点向量。现在解析此节点向量以获取所需信息。错误是这些行不正确:
完整的校正循环现在是: