-
Notifications
You must be signed in to change notification settings - Fork 1
/
sephora_scrapping.py
233 lines (200 loc) · 8.82 KB
/
sephora_scrapping.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
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
import sys
from urllib.request import urlopen
from urllib.request import FancyURLopener
import pandas as pd
import numpy as np
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome('/Users/ruizhang/Desktop/chromedriver')
def get_page_count(base_url):
browser = webdriver.Chrome('/Users/ruizhang/Desktop/chromedriver')
browser.get(base_url)
time.sleep(1)
page_count = 1
pages = browser.find_elements_by_class_name("css-x544ax")
if pages:
following_pages = browser.find_elements_by_class_name("css-1f9ivf5")
for page in following_pages:
count = int(page.text)
page_count = count
return page_count
def get_product_list(product_type, base_url):
browser = webdriver.Chrome('/Users/ruizhang/Desktop/chromedriver')
products_page_count = get_page_count(base_url)
product_list = []
for i in range(1, products_page_count + 1):
page_url = base_url + "?currentPage=" + str(i)
browser.get(page_url)
time.sleep(1)
elem = browser.find_element_by_tag_name("body")
no_of_pagedowns = 20
while no_of_pagedowns:
elem.send_keys(Keys.PAGE_DOWN)
time.sleep(0.2)
no_of_pagedowns -= 1
post_elems = browser.find_elements_by_class_name("css-ix8km1")
for post in post_elems:
product_name = post.get_attribute('aria-label')
product_link = post.get_attribute('href')
product_list.append([product_name, product_link])
df = pd.DataFrame(product_list, columns=['product_name', 'product_link'])
df.to_csv('tmp/'+product_type+'_product_list.csv', index=True)
def get_product_info_and_review_info(product_type, product_index, url):
browser.get(url)
# close pop-up login window
try:
login_window = browser.find_element_by_class_name('css-fslzaf')
login_window.click()
except (NoSuchElementException, ElementNotVisibleException) as exceptions:
pass
elem = browser.find_element_by_tag_name("body")
no_of_pagedowns = 20
while no_of_pagedowns:
elem.send_keys(Keys.PAGE_DOWN)
time.sleep(0.2)
no_of_pagedowns -= 1
# get product details (string)
product_details_panel = browser.find_element_by_id('tabpanel0')
product_details = product_details_panel.find_element_by_class_name('css-pz80c5').text
try:
total_reviews_text = browser.find_element_by_class_name('css-j9s1kd').text
total_reviews_count = total_reviews_text.split(' ')[0]
if total_reviews_count == '0':
product_info = [product_index, product_details, 0, 0, 0, 0, 0, 0, 0]
print(product_info)
return product_info
except NoSuchElementException:
pass
total_reviews_text = browser.find_element_by_class_name('css-mzsag6').text
total_reviews_count = total_reviews_text.split(' ')[0]
# get reviews count of different rates
table_id = browser.find_element_by_class_name('css-960eb6')
rows = table_id.find_elements_by_tag_name('tr')
rates = []
for row in rows:
# Get the columns (all the column 2)
cols = row.find_elements_by_tag_name('td') #note: index start from 0, 1 is col 2
rates.append(int(cols[2].text))
# get overall review rate
overall_review_text = browser.find_element_by_class_name('css-1eqf5yr').text
overall_review = float(overall_review_text.split(' ')[0])
product_info = [product_index, product_details, total_reviews_count, rates[0],rates[1],rates[2],rates[3],rates[4], overall_review]
print(product_info)
return product_info
def get_product_reviews(product_type, product_index, url):
browser.get(url)
# close pop-up login window
try:
login_window = browser.find_element_by_class_name('css-fslzaf')
login_window.click()
except (NoSuchElementException, ElementNotVisibleException) as exceptions:
pass
# t = browser.find_element_by_class_name('css-fslzaf')
# if t:
# t.click()
elem = browser.find_element_by_tag_name("body")
no_of_pagedowns = 20
while no_of_pagedowns:
elem.send_keys(Keys.PAGE_DOWN)
time.sleep(0.2)
no_of_pagedowns -= 1
# get total reviews count (int)
# total_reviews_text = browser.find_element_by_class_name('css-2rg6q7').text
# total_reviews_count = total_reviews_text.split(' ')[0]
try:
total_reviews_text = browser.find_element_by_class_name('css-j9s1kd').text
total_reviews_count = total_reviews_text.split(' ')[0]
if total_reviews_count == '0':
return
except NoSuchElementException:
pass
total_reviews_elem = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,"css-mzsag6")))
total_reviews_text = total_reviews_elem.text
total_reviews_count = total_reviews_text.split(' ')[0]
try:
next_comments = browser.find_element_by_class_name('css-1phfyoj')
max_load_count = 84
# load all reviews
while next_comments and max_load_count:
next_comments.click()
next_comments = browser.find_element_by_class_name('css-1phfyoj')
next_comments = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,"css-1phfyoj")))
max_load_count -= 1
except NoSuchElementException as exception:
pass
all_reviews_elements = browser.find_elements_by_css_selector("[data-comp=Review]")
columns = ['review_user_id', 'review_user_info', 'rate', 'review_date', 'review_content',
'not_helpful', 'helpful']
all_reviews = []
for review in all_reviews_elements:
# read info from each review
review_user_id = ''
try:
review_user_id = review.find_element_by_class_name('css-y7wmlg').text
except NoSuchElementException:
pass
review_user_info = ''
try:
all_review_user_info = review.find_elements_by_class_name('css-15415he')
review_user_info = ''
for r in all_review_user_info:
review_user_info += r.text
except NoSuchElementException:
pass
rate = ''
review_date = ''
review_content = ''
try:
rate = review.find_element_by_class_name('css-5quttm').get_attribute('aria-label')
except NoSuchElementException:
pass
try:
review_date = review.find_element_by_class_name('css-1mfxbmj').text
except NoSuchElementException:
pass
try:
review_content = review.find_element_by_class_name('css-1p4f59m').text
except NoSuchElementException:
pass
# if_recommend_this_product = review.find_element_by_class_name('css-ue839').text
helpful = ''
not_helpful = ''
try:
if_helpful = review.find_elements_by_class_name('css-lgjx3u')
not_helpful = if_helpful[0].text
helpful = if_helpful[1].text
except NoSuchElementException:
pass
all_reviews.append(
[review_user_id, review_user_info, rate, review_date, review_content,
not_helpful, helpful])
df = pd.DataFrame(all_reviews, columns=columns)
df.to_csv('tmp_data/' + product_type + "_" + str(product_index) + '.csv', index=False)
def get_all_products_info(product_type, all_products_list):
product_count = len(all_products_list)
all_products_info = []
# start_index = 7
# Get only the first 100 products' reviews
for i in range(min(100, product_count)):
print(all_products_list[i][1])
product_url = all_products_list[i][2]
get_product_reviews(product_type, all_products_list[i][0], product_url)
# product_info = get_product_info_and_review_info(product_type, i+start_index, product_url)
# all_products_info.append(product_info)
# df = pd.DataFrame(all_products_info, columns=['product_index', 'product_details', 'total_reviews_count',
# '5_star_counts','4_star_counts','3_star_counts','2_star_counts',
# '1_star_counts', 'overall_review'])
# df.to_csv('tmp_data/'+product_type + '_all_products_info.csv', index=False)
if __name__ == '__main__':
eye_cream_df = pd.read_csv('tmp_data/eye_cream_product_list_part.csv')
eye_cream_list = eye_cream_df.values.tolist()
get_all_products_info('eye_cream',eye_cream_list)