-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripadvisor.py
54 lines (46 loc) · 1.82 KB
/
tripadvisor.py
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
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from bs4 import BeautifulSoup
class TripAdvisor:
def __init__(self):
self.id_to_rating = { 'bubble_00':0, 'bubble_05':0.5, 'bubble_10':1.0,
'bubble_15':1.5, 'bubble_20':2.0, 'bubble_25':2.5, 'bubble_30':3.0,
'bubble_35':3.5, 'bubble_40':4.0, 'bubble_45':4.5, 'bubble_50':5.0 }
self.sent = SentimentIntensityAnalyzer()
def sentiment(self, text):
if str((self.sent.polarity_scores(text))['compound'])[0] == '-':
return text.upper()
else: return text
def name(self, soup, source='trip'):
x = soup.find('h1', class_='YeV2SlB6 propertyHeading')
if x: return x.text
else: return None
def no_of_reviews(self, soup, source='trip'):
x = soup.find('div', class_='ui_poi_review_rating')
if x: return ((soup.find('div', class_='ui_poi_review_rating').text).split(' '))[0]
else: return None
def overall_score(self, soup, source='trip'):
x = soup.find('div', class_='ui_poi_review_rating')
if x:
elem = x.find('span', class_='ui_bubble_rating')
return self.id_to_rating[(elem['class'])[1]]
else: return None
def last_review_score(self, soup, source='trip'):
last_rating = soup.find('div', class_='rating')
if last_rating:
return self.id_to_rating[((last_rating.find('span', class_='ui_bubble_rating'))['class'])[1]]
else:
return None
def last_review_date(self, soup, source='trip'):
last_rating = soup.find('div', class_='rating')
if last_rating: return last_rating.find('span', class_='ratingDate')['title']
else: return None
def get_reviews(self, soup, source='trip'):
reviews = []
revs = soup.find_all('div', class_='review-container')
if revs:
for x in revs:
for y in x.find('div', class_='prw_rup prw_reviews_text_summary_hsx'):
reviews.append(self.sentiment(y.text))
return reviews[:5]
else:
return None