-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapper.py
289 lines (259 loc) · 10.7 KB
/
scrapper.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import argparse
import email_notificator
import json
import logging
import pandas
import requests
import telegram_send
import os
from bs4 import BeautifulSoup
from datetime import datetime
from random import randint
HEADERS = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avi"
"f,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9,pl;q=0.8",
"cache-control": "max-age=0",
"device-memory": "8",
"dnt": "1",
"downlink": "10",
"dpr": "1",
"ect": "4g",
"rtt": "100",
"sec-ch-device-memory": "8",
"sec-ch-dpr": "1",
"sec-ch-ua": "^\^Google",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\^Windows^^",
"sec-ch-ua-platform-version": "^\^10.0.0^^",
"sec-ch-viewport-width": "910",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
"viewport-width": "910"
}
def load_proxies():
logging.info('Loading proxies..')
raw_request = requests.get(
"https://raw.githubusercontent.com/mertguvencli/http-proxy-list/main/proxy-list/data.json").text
proxies_list = json.loads(raw_request)
return proxies_list
def notify_telegram(product_name, price, link, price_diff, config_path=None):
try:
message = f'''
We found match for one of your items:\n
[{product_name}] for {price}\n
Link: {link}\n
It\'s {price_diff} cheaper than your target price!
'''
if config_path:
telegram_send.send(messages=[message], conf=config_path)
else:
telegram_send.send(messages=[message])
except Exception as e:
logging.error(f'telegram_send wasnt configured propertly, error:\n{e}')
def format_price(price_str_):
if price_str_ is not None:
dot_sep = price_str_.find(".")
coma_sep = price_str_.find(",")
if coma_sep > 0:
price_str_ = price_str_[:coma_sep]
elif dot_sep > 0:
price_str_ = price_str_[:dot_sep]
try:
price = int(''.join(i for i in price_str_ if i.isdigit()))
except ValueError:
# temp solution, just crazy huge price for comparasion
price = 9999999
return price
else:
# temp solution, just crazy huge price for comparasion
return 9999999
def get_prod_title(soup_obj):
try:
product_name = soup_obj.find(id="productTitle").get_text().strip()
return product_name
except AttributeError:
logging.debug(
f'Couldnt find product name for {link}')
# If can't get Product title, then skips product
return None
def check_prod_availability(soup_obj):
"""Function checks product's availability, returns True if is available"""
try:
soup_obj.select("#outOfStock .a-text-bold")[0].get_text()
logging.debug(f"[{link}] out of stock, skipping..")
return False
# If available - checking warehouse price
except IndexError:
logging.debug(f"[{link}] in stock, checking warehouse price..")
return True
def get_detail_price(soup_obj):
logging.debug("Checking detail price..")
try:
div = '#newAccordionRow_0 .a-price-whole'
price_str = soup_obj.select(div)[0].get_text()[:6]
price = format_price(price_str)
return price
except IndexError:
logging.debug("Retrying with detail price..")
try:
div = '#corePrice_feature_div .a-price-whole'
price_str = soup_obj.select(div)[0].get_text()[:6]
price = format_price(price_str)
return price
except IndexError:
logging.debug("Couldn't find detail price for, skipping..")
return None
def get_wh_price(el_name, soup_obj):
try:
element = soup_obj.select(el_name)[0].get_text()[:6]
except IndexError:
# when didn't find anything
return None
return element
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--config",
help="Provide absolute path to the custom telegram_send config file.",
type=str,
required=False
)
parser.add_argument('--notify', type=str, required=True,
choices=['mail', 'no-notify', 'telegram'])
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
log_lvl = logging.INFO
if args.debug:
log_lvl = logging.DEBUG
logging.basicConfig(
level=log_lvl,
filename='logs.log',
format="%(levelname)s : %(asctime)s : %(message)s"
)
config_path = os.path.abspath("config")
search_data_path = os.path.abspath("search_data")
# Reading file
products = pandas.read_csv(f'{config_path}/tracked_products.csv', sep=',')
date_now = datetime.now().strftime('%Y-%m-%d %H:%M')
recent_data = pandas.DataFrame()
proxy_list = load_proxies()
for prod_number, link in enumerate(products.link):
# loop for each item to brute force through errors
while True:
rand_prxy = proxy_list[randint(0, len(proxy_list)-1)]
proxy = {
"http": f"http://{rand_prxy['ip']}:{rand_prxy['port']}",
"https": f"https://{rand_prxy['ip']}:{rand_prxy['port']}"
}
logging.debug(f"Trying with proxy: {rand_prxy['ip']}:{rand_prxy['port']}")
try:
page = requests.get(products.link[prod_number], proxies=proxy, headers=HEADERS, timeout=15)
# page = requests.get(products.link[prod_number], proxies=proxy, headers=HEADERS)
soup = BeautifulSoup(page.content, features='lxml')
if "To discuss automated access to Amazon data" in str(soup):
logging.warning("Cooldown from amazon applied, trying different proxy..")
elif "This item cannot be shipped to your selected delivery location" in str(soup):
logging.warning("Wrong proxy location for item, trying different proxy..")
else:
break
except TimeoutError:
logging.warning("Proxy or destination server timed out, retrying..")
except requests.exceptions.ConnectTimeout:
logging.warning("Proxy or destination server timed out, retrying..")
except requests.exceptions.ProxyError:
logging.warning("Proxy error happened, retrying w/ different one..")
except requests.exceptions.ConnectionError:
logging.warning("Cennection error happened, retrying w/ different proxy..")
except requests.exceptions.SSLError:
logging.warning("SSLError happened, retrying w/ different proxy..")
product_name = get_prod_title(soup)
# If couldn't find prod name
if product_name is None:
continue
# Checking if item is available
if not check_prod_availability(soup):
continue
else:
# Giving a try for different divs, and taking cheapest.
div_a = '#olpLinkWidget_feature_div .a-color-base'
price_str_a = get_wh_price(div_a, soup)
div_b = '#usedAccordionRow .a-price-whole'
price_str_b = get_wh_price(div_b, soup)
div_c = '#olpLinkWidget_feature_div .a-price-whole'
price_str_c = get_wh_price(div_c, soup)
# Taking lowest price from different tries
price = min(
format_price(price_str_a),
format_price(price_str_b),
format_price(price_str_c)
)
if price == 9999999:
price = get_detail_price(soup)
if price is None:
continue
# Sending email notifications if price is lower than our Target price
try:
if price < products.target_price[prod_number] \
and args.notify == "mail":
email_notificator.send_mail(
product_name=product_name,
price=price,
link=products.link[prod_number],
price_diff=round(
products.target_price
[prod_number] - price, 2)
)
logging.info(
f'Sending email notification about '
f'[{product_name}] for: {price}')
elif price < products.target_price[prod_number] \
and args.notify == "telegram":
if args.config:
notify_telegram(
product_name=product_name,
price=price,
link=products.link[prod_number],
price_diff=round(
products.target_price
[prod_number] - price, 2),
config_path=args.config
)
else:
notify_telegram(
product_name=product_name,
price=price,
link=products.link[prod_number],
price_diff=round(
products.target_price
[prod_number] - price, 2)
)
logging.info(
f'Sending telegram notification about '
f'[{product_name}] for: {price}')
except TypeError:
pass
if price < 9999999:
logging.info(f'Finally found [{product_name}] for: {price}')
data = pandas.DataFrame({
'Date': date_now,
'Link': link,
'Product name': product_name,
'Target price': products.target_price[prod_number],
'Actual price': price
}, index=[prod_number])
recent_data = recent_data.append(data)
logging.info(f'Patching [{product_name}] to data set.')
# logging only when found anything
if not recent_data.empty:
previous_searches = f'{search_data_path}/searching_history.xlsx'
previous_data = pandas.read_excel(previous_searches)
logging.info('Collecting previous data.')
complete_data = previous_data.append(recent_data, sort=False)
logging.info('Appending recent data, and merging with previous data.')
complete_data.to_excel('search_data/searching_history.xlsx', index=False)