-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_scrapping.py
More file actions
240 lines (167 loc) · 7.46 KB
/
web_scrapping.py
File metadata and controls
240 lines (167 loc) · 7.46 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
# coding: utf-8
# In[1]:
"""Part I. Code to pull key information from alltrails.com and create a 'data_raw.csv' to work with"""
import selenium #for web scrapping
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait # for the elements to be visible
from selenium.webdriver.common.by import By #Explicit Waits
from selenium.webdriver.support import expected_conditions as EC #Explicit Waits
from bs4 import BeautifulSoup
import requests
import pandas as pd
#from pymongo import MongoClient
import time
# In[2]:
# Función para hacer login: debo estar logueada para poder navegar varias páginas
def login_and_get_soup(navegador):
navegador.get('http://www.alltrails.com')
go_login = navegador.find_element_by_xpath('//li[@id="login"]')
go_login.click()
time.sleep(9)
username = navegador.find_element_by_id("user_email")
password = navegador.find_element_by_id("user_password")
username.send_keys("paola.aleanflorez15@bathspa.ac.uk")
password.send_keys("paopao")
navegador.find_element_by_name("commit").click()
navegador.get('https://www.alltrails.com/es/mexico')
soup = get_hike_routes(navegador)
return soup, navegador
# In[3]:
# Función que carga todas las reseñas de la página:
def get_hike_routes(navegador):
navegador.get('https://www.alltrails.com/es/mexico')
while True:
try:
load_more_hikes = WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.XPATH,'//div[@id="load_more"]')))
load_more_hikes.click()
time.sleep(5)
except:
break
soup = BeautifulSoup(navegador.page_source) #getting all the HTML soup
return soup
# In[12]:
# Función que 'scrapea' cada link (252) con el raw data para el db
def parsing_data(html_soup):
header = html_soup.find('div', id='title-and-menu-box') # Header where it can be found: hike_name, difficulty_level, stars and num_reviews
region = html_soup.find_all('span',{'class':'xlate-none'})[3].text.lower() #zone name
try:
hike_name = header.findChild('h1').text
except:
hike_name = None
difficulty_level = header.findChild('span').text.lower()
stars = header.findChild('meta')['content']
num_reviews = header.find('span', itemprop="reviewCount").text
try: # Distance of the route
distance = html_soup.select('span.distance-icon')[0].text
except:
distance = None
try: # Elevation gain of the route
elevation = html_soup.select('span.elevation-icon')[0].text
except:
elevation = None
try: # Route type, if it is circular or not
route_type = html_soup.select('span.route-icon')[0].text
except:
route_type = None
tags = html_soup.select('section.tag-cloud')[0].findChildren('h3') # Tags with route characteristics
hike_attributes = [tag.text for tag in tags]
users = html_soup.select('div.feed-user-content.rounded') # List of dictionaries with the user name and rating number
user_ratings = []
for user in users:
if user.find('span', itemprop='author') != None:
user_name = user.find('span', itemprop='author').text
#user_name = user_name.replace('.', '')
try:
rating = user.find('span', itemprop="reviewRating").findChildren('meta')[0]['content']
user_ratings.append({user_name: rating})
except:
pass
row_data = {}
row_data['hike_name'] = hike_name
row_data['region'] = region
row_data['difficulty_level'] = difficulty_level
row_data['stars'] = stars
row_data['num_reviews'] = num_reviews
row_data['distance'] = distance
row_data['elevation'] = elevation
row_data['route_type'] = route_type
row_data['hike_attributes'] = hike_attributes
row_data['user_ratings'] = user_ratings
return row_data
# In[13]:
#Función que crea el database a partir de los vínculos, llama la función de 'parsing_data' y los convierte 252 html_soups en db
hike_list =[]
def create_db(soup, navegador):
resenas = navegador.find_elements_by_class_name('mobile-block') # Points to the titles of mountain routes
vinculos = [el.get_attribute('href') for el in resenas] # It goes through the ratings and get the routes link
vinculos_unique = list(set(vinculos)) # Unique list of hike links, ready to scrap <<252 routes with given rating>>
for el in vinculos_unique:
html = requests.get(el).content
html_soup = BeautifulSoup(html,'html')
hike = parsing_data(html_soup)
hike_list.append(hike)
#table.insert_one(mongo_doc)
return hike_list
# In[14]:
# PROBANDO - segunda parte:
#hike = create_db(soup, navegador)
# In[15]:
#create_db(soup, navegador)
# In[16]:
#len(hike_list)
# In[17]:
def empty_df():
# Creating empty df with column titles
df = pd.DataFrame(columns=['hike_name',
'region',
'difficulty_level',
'stars',
'num_reviews',
'distance',
'elevation',
'route_type',
'hike_attributes',
'user_ratings'])
return df
def parse_record(hike_list):
# Getting each row
row = pd.Series({'hike_name': hike_list.get('hike_name', None),
'region': hike_list.get('region', None),
'difficulty_level': hike_list.get('difficulty_level', None),
'stars': hike_list.get('stars', None),
'num_reviews': hike_list.get('num_reviews', None),
'distance': hike_list.get('distance', None),
'elevation': hike_list.get('elevation', None),
'route_type': hike_list.get('route_type', None),
'hike_attributes': hike_list.get('hike_attributes', None),
'user_ratings': hike_list.get('user_ratings', None)})
return row
def turn_into_pandas(hike_list):
'''
Function to pull 'hike' from the raw_data into a pandas DataFrame
INPUT: 'hike' from the raw_data
OUTPUT: pandas DataFrame object
'''
df = empty_df()
df_2 = empty_df()
i = 0
for h in hike_list:
i += 1
row = parse_record(h)
df_2 = df_2.append(row, ignore_index=True)
df = df.append(df_2)
return df
# In[18]:
if __name__ == '__main__':
navegador = webdriver.Chrome() # Abre un nuevo navegador
soup = login_and_get_soup(navegador) # Hace login, y consigue los links de las rutas to scrap
create_db(soup, navegador) # Crea un db con row_data, retorna 'hike_list'
hike_df = turn_into_pandas(hike_list)
hike_df
# In[20]:
hike_df.to_csv('data_raw.csv',index=False)
# In[21]:
hike_df.head()
# In[ ]: