-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_price_tracker.py
91 lines (65 loc) · 2.44 KB
/
twilio_price_tracker.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
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from twilio.rest import Client
account_sid = "AC93642d372e8e6635f97a1f70e9010b77"
auth_token = "8a6a967188a6e1b6c7658b113c187246"
client = Client(account_sid, auth_token)
def get_url(search_text):
"""Generate a url from search text"""
url = 'https://www.amazon.com/s?k={}&ref=nb_sb_noss_1'.format(search_text)
# add page query
url += '&page{}'
return url
def extract_record(single_record):
"""Extract and return data from a single record"""
# Title
title_tag = single_record.h2.a
title = title_tag.text.strip()
url = 'https://www.amazon.com' + title_tag.get('href')
# because some products dont have prices we have to
# use try-except block to catch AttributeError
try:
# product price
price_parent = single_record.find('span', 'a-price')
price = price_parent.find('span', 'a-offscreen').text
except AttributeError:
return
result = (title, price)
return result
def main(search_term, max_price):
"""Run main program routine"""
# startup the webdriver
options=Options()
options.headless = True #choose if we want the web browser to be open when doing the crawling
# options.use_chromium = True
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
records = []
prices_list=[]
url = get_url(search_term)
for page in range(1, 3):
driver.get(url.format(page))
soup = BeautifulSoup(driver.page_source, 'html.parser')
results = soup.find_all('div', {'data-component-type': 's-search-result'})
for item in results:
record = extract_record(item)
if record:
records.append(record)
for titles, prices in records:
prices_list.append(prices)
new_prices= [s.replace("$", "").replace(',',"") for s in prices_list]
print(new_prices)
new=[]
prices_float = [float(i) for i in new_prices]
for i in prices_float:
if i < max_price:
new.append(i)
# print(new)
client.messages.create(
# to send SMS to mobile phone
to="+2348112398610",
from_="+14692840596",
body=f"There are {len(new)} rolex watchs less than than amount"
)
main("rolex", 10000)