-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scraping TripAdvisor - Partial beacuse of loops.R
205 lines (164 loc) · 6.59 KB
/
Scraping TripAdvisor - Partial beacuse of loops.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#-- Scraping TripAdvisor --#
#-- https://www.worldfullofdata.com/basics-web-scraping-r-rvest/ --#
library(rvest)
library(tidyverse)
library(tidylog)
# Read the web page and get the number of reviews
tripadvisor_restaurant <- read_html("https://www.tripadvisor.com/Restaurant_Review-g60763-d457808-Reviews-Daniel-New_York_City_New_York.html")
totalReviews <- tripadvisor_restaurant %>%
html_nodes(".reviewCount") %>%
html_text()
totalReviews <- totalReviews[1]
totalReviews <- strsplit(totalReviews, " ")[[1]][1]
totalReviews <- as.numeric(gsub(",", "", totalReviews))
# Get the cuisine and location of the restaurant
cuisine <- tripadvisor_restaurant %>%
html_node(".cuisines .text") %>%
html_text()
cuisine
#[1] " French, Vegetarian Friendly, Vegan Options, Gluten Free Options "
streetAddress <- tripadvisor_restaurant %>%
html_node("span.street-address") %>%
html_text()
streetAddress
#[1] "60 East 65th Street"
locality <- tripadvisor_restaurant %>%
html_node("span.locality") %>%
html_text()
locality
#[1] "New York City, NY 10065"
name <- tripadvisor_restaurant %>%
html_node(".ui_header") %>%
html_text()
name
# Get the text of the services and its rating
ratingServiceText <- tripadvisor_restaurant %>%
html_nodes(".restaurants-detail-overview-cards-RatingsOverviewCard__overallRating--nohTl") %>%
html_text()
ratingServiceText
#4.5
## FROM IMAGE GET ATTRIBUTE !!!
ratingServiceValue <- tripadvisor_restaurant %>%
html_nodes(".ui_bubble_rating") %>%
html_attr("alt")
ratingServiceValue
#[1] "4.5 of 5 bubbles" "4.5 of 5 bubbles" "4.0 of 5 bubbles" "4.5 of 5 bubbles"
ratingServiceValue <- strsplit(ratingServiceValue, " ")
ratingServiceValue <- as.numeric(lapply(ratingServiceValue, `[[`, 1))
ratingServiceValue
#[1] 4.5 4.5 4.0 4.5
#-- combine all the scraped data into a data frame with one row.
record_restaurant <- data.frame(name = name,
locality = locality,
streetAddress = streetAddress,
# cuisine = cuisine,
# ratingOverall = overallRating,
ratingService = as.list(setNames(ratingServiceValue, ratingServiceText)),
# ratingOverall = as.list(setNames(ratingValue, ratingText)),
totalReviews = totalReviews
)
#LOOP THROUGH OBJECTS AND PAGES
# Read html and select the URL's for each restaurant
tripadvisor_home <- read_html("https://www.tripadvisor.com/Restaurants-g60763-New_York_City_New_York.html")
restaurant_URLs <- tripadvisor_home %>%
html_nodes(".restaurants-list-ListCell__restaurantName--2aSdo") %>%
html_attr("href")
restaurant_URLs
#loop through the 30 URL’s to get the information per restaurant .
# Loop through 30 restaurants and combine this into one data frame
library(plyr) # needed for rbind.fill function
get30Restaurants <- function(restaurant_URLs){
data <- data.frame()
i=1
for(i in 1:length(restaurant_URLs)){
tripadvisor_restaurant <- read_html(paste0("https://www.tripadvisor.com", restaurant_URLs[i]))
record_restaurant <- getRestaurant(tripadvisor_restaurant)
data <- rbind.fill(data, record_restaurant)
print(i)
}
data
}
df30Restaurants <- get30Restaurants(restaurant_URLs)
#------------
library(rvest)
library(plyr)
tripadvisor_home <- html_session("https://www.tripadvisor.com/Restaurants-g60763-New_York_City_New_York.html")
getRestaurant <- function(tripadvisor_restaurant){
name <- tripadvisor_restaurant %>%
html_nodes(".heading_title") %>%
html_text()
overallRating <- tripadvisor_restaurant %>%
html_nodes(".overallRating") %>%
html_text() %>%
as.numeric()
totalReviews <- tripadvisor_restaurant %>%
html_nodes(".seeAllReviews") %>%
html_text()
totalReviews <- strsplit(totalReviews, " ")[[1]][1]
totalReviews <- as.numeric(gsub(",", "", totalReviews))
streetAddress <- tripadvisor_restaurant %>%
html_node(".address .street-address") %>%
html_text()
locality <- tripadvisor_restaurant %>%
html_node(".address .locality") %>%
html_text()
cuisine <- tripadvisor_restaurant %>%
html_node(".cuisines .text") %>%
html_text()
ratingText <- tripadvisor_restaurant %>%
html_nodes("#ratingFilter .row_label") %>%
html_text()
ratingValue <- tripadvisor_restaurant %>%
html_nodes("#ratingFilter span") %>%
html_text()
ratingValue <- as.numeric(ratingValue[seq(4, length(ratingValue), 5)])
ratingServiceText <- tripadvisor_restaurant %>%
html_nodes(".ratingSummary .text") %>%
html_text()
ratingServiceValue <- tripadvisor_restaurant %>%
html_nodes(".ratingSummary .ui_bubble_rating") %>%
html_attr("alt")
ratingServiceValue <- strsplit(ratingServiceValue, " ")
ratingServiceValue <- as.numeric(lapply(ratingServiceValue, `[[`, 1))
record_restaurant <- data.frame(name = name,
locality = locality,
streetAddress = streetAddress,
cuisine = cuisine,
ratingOverall = overallRating,
ratingService = as.list(setNames(ratingServiceValue, ratingServiceText)),
ratingOverall = as.list(setNames(ratingValue, ratingText)),
totalReviews = totalReviews
)
record_restaurant
}
get30Restaurants <- function(restaurant_URLs){
data <- data.frame()
i=1
for(i in 1:length(restaurant_URLs)){
tripadvisor_restaurant <- read_html(paste0("https://www.tripadvisor.com", restaurant_URLs[i]))
record_restaurant <- getRestaurant(tripadvisor_restaurant)
data <- rbind.fill(data, record_restaurant)
print(i)
}
data
}
get30RestaurantsXpages <- function(tripadvisor_home, X){
data <- data.frame()
i = 1
for(i in 1:X){
if(i != 1){ # Go to next page but don't skip the first page
next_URL <- tripadvisor_home %>%
html_nodes(".nav.next") %>%
html_attr("href")
tripadvisor_home <- jump_to(tripadvisor_home, paste0("https://www.tripadvisor.com", next_URL))
}
restaurant_URLs <- tripadvisor_home %>%
html_nodes(".property_title") %>%
html_attr("href")
df30Restaurants <- get30Restaurants(restaurant_URLs)
data <- rbind.fill(data, df30Restaurants)
print(paste0("Page ", i))
}
data
}
restaurants <- get30RestaurantsXpages(tripadvisor_home, 2)