forked from aesuli/trip-advisor-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrip-advisor-parser.py
114 lines (97 loc) · 4.22 KB
/
trip-advisor-parser.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Andrea Esuli ([email protected])
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import codecs
import csv
import fnmatch
import html
import os
import re
def get_review_filesnames(input_dir):
for root, dirnames, filenames in os.walk(input_dir):
for filename in fnmatch.filter(filenames, '*.html'):
yield os.path.join(root, filename)
def cleanhtml(htmltext):
cleanre = re.compile('<.*?>')
cleantext = re.sub(cleanre, ' ', htmltext)
return cleantext
summaryre = re.compile(r'innerBubble(.*?)ReportIAP', re.M | re.S)
# old format
# overallratingre = re.compile(r'class="sprite-rating_s_fill rating_s_fill s([0-9])0\"')
overallratingre = re.compile(r'reviewItemInline.*?bubble_([0-9])', re.M | re.S)
reviewtextre = re.compile(r'<div class="entry">(.*?)</div>', re.M | re.S)
aspectre = re.compile(r'recommend-answer(.*?)</li>', re.M | re.S)
# old format
# aspectratingre = re.compile(r'sprite-rating_ss_fill rating_ss_fill ss([0-9])0')
aspectratingre = re.compile(r'bubble_([0-9])')
# old format
# aspectnamere = re.compile(r'span>(.*)$', re.M | re.S)
aspectnamere = re.compile(r'recommend-description">(.*)</div$', re.M | re.S)
# old format, sometimes still used
oldhotelnamere = re.compile(r'warLocName">(.*)?</div>')
althotelnamere = re.compile(r'"description" content="(.*)?:')
hotelnamere = re.compile(r'title: "(.*)?"')
idre = re.compile(r'id="rn([0-9]+)"')
def get_aspect_ratings(block):
rates = list()
for aspectrating in aspectre.findall(block):
rating = aspectratingre.findall(aspectrating)[0]
name = aspectnamere.findall(aspectrating)[0].strip()
rates.append(':'.join([name, rating]))
return u';'.join(rates)
def main():
parser = argparse.ArgumentParser(
description='TripAdvisor Hotel parser')
parser.add_argument('-d', '--dir', help='Directory with the data for parsing', required=True)
parser.add_argument('-o', '--outfile', help='Output file path for saving the reviews in csv format', required=True)
args = parser.parse_args()
reviews = set()
with codecs.open(args.outfile, 'w', encoding='utf8') as out:
writer = csv.writer(out, lineterminator='\n')
for filepath in get_review_filesnames(args.dir):
with codecs.open(filepath, mode='r', encoding='utf8') as file:
htmlpage = file.read()
print(filepath)
try:
hotelname = hotelnamere.findall(htmlpage)[0].strip()
except IndexError:
try:
hotelname = oldhotelnamere.findall(htmlpage)[0].strip()
except IndexError:
hotelname = althotelnamere.findall(htmlpage)[0].strip()
for block in summaryre.findall(htmlpage):
try:
id_ = idre.findall(block)[0]
except IndexError:
continue
if id_ in reviews:
continue
reviews.add(id_)
reviewtext = reviewtextre.findall(block)[0]
overallrating = int(overallratingre.findall(block)[0])
if overallrating >= 4:
binaryrating = 'positive'
else:
binaryrating = 'negative'
reviewtext = cleanhtml(reviewtext).strip()
try:
reviewtext = html.unescape(reviewtext)
except Exception:
pass
review = [id_, filepath, hotelname, reviewtext, overallrating, binaryrating]
writer.writerow(review)
if __name__ == '__main__':
main()