-
Notifications
You must be signed in to change notification settings - Fork 1
/
prices_hotdealvn.py
230 lines (205 loc) · 7.55 KB
/
prices_hotdealvn.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
import sys
import os, glob
from zipfile import ZipFile
import time
import datetime
import schedule
import re
import csv
import random
import logging
from rich.logging import RichHandler
from rich.progress import track
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import signal
# Parameters
SITE_NAME = "hotdealvn"
BASE_URL = "https://www.hotdeal.vn/"
PROJECT_PATH = re.sub("/py$", "", os.getcwd())
PATH_HTML = PROJECT_PATH + "/html/" + SITE_NAME + "/"
PATH_CSV = PROJECT_PATH + "/csv/" + SITE_NAME + "/"
PATH_LOG = PROJECT_PATH + "/log/"
DATE = str(datetime.date.today())
OBSERVATION = 0
# Selenium options
OPTIONS = Options()
OPTIONS.add_argument('--headless')
OPTIONS.add_argument('--disable-gpu')
CHROME_DRIVER = PROJECT_PATH + "/bin/chromedriver" # Chromedriver v2.38
# Setting up logging
logging.basicConfig(
level="INFO",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(rich_tracebacks=True)]
)
log = logging.getLogger("rich")
# Defining main functions
def main():
try:
daily_task()
except Exception as e:
log.exception('Got exception, scraper stopped')
log.info(type(e).__name__ + str(e))
# Compress data and html files
compress_csv()
compress_html()
log.info('Finished. Hibernating until next day...')
def daily_task():
"""Main workhorse function. Support functions defined below"""
global CATEGORIES_PAGES
global BROWSER
global DATE
global OBSERVATION
log.info('Scraper started')
# Refresh date
DATE = str(datetime.date.today())
OBSERVATION = 0
# Initiate headless web browser
log.info('Initializing browser')
BROWSER = webdriver.Chrome(executable_path=CHROME_DRIVER,
options=OPTIONS)
# Download topsite and get categories directories
base_file_name = "All_cat_" + DATE + ".html"
fetch_html(BASE_URL, base_file_name, PATH_HTML, attempts_limit=1000)
html_file = open(PATH_HTML + base_file_name).read()
CATEGORIES_PAGES = get_category_list(html_file)
log.info('Found ' + str(len(CATEGORIES_PAGES)) + ' categories')
# Read each categories pages and scrape for data
for cat in track(CATEGORIES_PAGES,
description = "[green]Scraping...",
total = len(CATEGORIES_PAGES)):
cat_file = "cat_" + cat['name'] + "_" + DATE + ".html"
download = fetch_html(cat['directlink'], cat_file, PATH_HTML)
if download:
scrap_data(cat)
# Close browser
BROWSER.close()
BROWSER.service.process.send_signal(signal.SIGTERM)
BROWSER.quit()
def fetch_html(url, file_name, path, attempts_limit=5):
"""Fetch and download a html with provided path and file names"""
if not os.path.exists(path):
os.makedirs(path)
if os.path.isfile(path + file_name) is False:
attempts = 0
while attempts < attempts_limit:
try:
BROWSER.get(url)
element = BROWSER.find_element_by_xpath("/html")
html_content = element.get_attribute("innerHTML")
with open(path + file_name, "w") as f:
f.write(html_content)
log.debug(f"Downloaded: {file_name}")
return(True)
except:
attempts += 1
log.warning("Try again" + file_name)
else:
log.error(f"Cannot download {file_name}")
return(False)
else:
log.debug(f"Already downloaded {file_name}")
return(True)
def get_category_list(top_html):
"""Get list of relative categories directories from the top page"""
page_list = []
toppage_soup = BeautifulSoup(top_html, "lxml")
categories = toppage_soup.findAll("ul", attrs={'class': 'nav main-nav'})
categories_tag = [cat.findAll('a', {'href': re.compile(".+")})
for cat in categories]
categories_tag = [item for sublist in categories_tag for item in sublist]
for cat in categories_tag:
next_page = {}
link = re.sub(".+hotdeal\.vn", "", cat['href'])
next_page['relativelink'] = link
next_page['directlink'] = BASE_URL + link
next_page['name'] = re.sub("/|\\?.=", "_", link)
next_page['label'] = re.sub("\\n", "", cat.text)
page_list.append(next_page)
# Remove duplicates
page_list = [dict(t) for t in set(tuple(i.items()) for i in page_list)]
return(page_list)
def scrap_data(cat):
"""Get item data from a category page.
Requires downloading the page first.
"""
global OBSERVATION
soup = BeautifulSoup(BROWSER.page_source, 'lxml')
cat_div = soup.find("div", {"class": "row products"})
if cat_div:
cat_div = cat_div.findAll("div", {"class": re.compile("product-kind")})
else:
cat_div = []
data = []
for item in cat_div:
row = {}
good_name = item.find('h3', {"class": "product__title"})
row['good_name'] = good_name.a.get("title") if good_name else None
price_tag = item.find('span', {'class': 'price__value'})
row['price'] = price_tag.contents[0] if price_tag else None
old_price_tag = item.find('div', {'class': '_product_price_original'})
old_price_tag = old_price_tag.find("span", {"class": "price__value"}) \
if old_price_tag else None
row['old_price'] = old_price_tag.contents[0] if old_price_tag else None
row['id'] = item.get('id')
row['category'] = cat['name']
row['category_label'] = cat['label']
row['date'] = DATE
OBSERVATION += 1
write_data(row)
def write_data(item_data):
"""Write an item data as a row in csv. Create new file if needed"""
fieldnames = ['good_name', 'price', 'old_price', 'id',
'category', 'category_label', 'date']
file_exists = os.path.isfile(PATH_CSV + SITE_NAME + "_" + DATE + ".csv")
if not os.path.exists(PATH_CSV):
os.makedirs(PATH_CSV)
with open(PATH_CSV + SITE_NAME + "_" + DATE + ".csv", "a") as f:
writer = csv.DictWriter(f, fieldnames)
if not file_exists:
writer.writeheader()
writer.writerow(item_data)
def compress_csv():
"""Compress downloaded .csv files"""
if not os.path.exists(PATH_CSV):
os.makedirs(PATH_CSV)
os.chdir(PATH_CSV)
try:
zip_csv = ZipFile(SITE_NAME + '_' + DATE + '_csv.zip', 'a') #
for file in glob.glob("*" + DATE + "*" + "csv"):
zip_csv.write(file)
os.remove(file)
log.info(f"Compressing {str(OBSERVATION)} item(s)")
except Exception as e:
log.error('Error when compressing csv')
log.info(type(e).__name__ + str(e))
os.chdir(PROJECT_PATH)
def compress_html():
"""Compress downloaded .html files"""
if not os.path.exists(PATH_HTML):
os.makedirs(PATH_HTML)
os.chdir(PATH_HTML)
try:
zip_csv = ZipFile(SITE_NAME + '_' + DATE + '_html.zip', 'a') #
for file in glob.glob("*" + DATE + "*" + "html"):
zip_csv.write(file)
os.remove(file)
log.info("Compressing HTML files")
except Exception as e:
log.error('Error when compressing html')
log.info(type(e).__name__ + str(e))
os.chdir(PROJECT_PATH)
# Run scripts if argument is 'test', run and hibernate if 'run' else hibernate
if "test" in sys.argv:
main()
else:
if "run" in sys.argv:
main()
start_time = '01:' + str(random.randint(0,59)).zfill(2)
schedule.every().day.at(start_time).do(main)
while True:
schedule.run_pending()
time.sleep(1)