Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70c3986

Browse files
Merge pull request avinashkranjan#71 from sarthak1905/amazon-alert
Added Project
2 parents 56e42c9 + c492bff commit 70c3986

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

‎amazon-price-alert/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Amazon prices keep fluctuating- Let's scrape them!
2+
### Developed by [Sarthak Saxena](https://github.com/sarthak1905)
3+
C3PO is a web-scraper built on BeautifulSoup that alerts you when the price of an amazon prduct falls within your budget! Currently in development.
4+
5+
## What is the use?
6+
Nothing is worse than seeing the price drop of a product you just bought on Amazon drop 2 days after you bought it. Wouldn't it be amazing if a python script could just
7+
ask you your budget and the product link and then notify you when the product price drops below that? This is exactly what this script does!
8+
9+
## How to use
10+
* Simply copy the link of the product and your budget that's it!
11+
* Paste the link when the program requests you to
12+
* Enter budget for the product(without currency symbol)
13+
* Select frequency to check prices
14+
* Keep the script running in the background
15+
The scraper will do the rest for you and notify you when the price is in your budget.
16+
However, it is to be kept in mind that the sender email ID and password has been stored in os variables in the script. Therefore, wherever you see the imports of
17+
'DEVELOPER_EMAIL' and 'DEVELOPER_PASS', be sure to replace them with the email ID you wish to recieve the notification from.
18+
### Side Note
19+
Do remember to install the dependencies in the requirements.txt file!
20+
21+
## Modules used (available in requirements.txt)
22+
* requests_html
23+
* BeautifulSoup
24+
25+
## Development Status
26+
This scrapper is complete. A future version may have emails sent via a server.

‎amazon-price-alert/amazon_scraper.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Developed and maintained by https://github.com/sarthak1905
2+
from bs4 import BeautifulSoup
3+
from requests_html import HTMLSession
4+
import os
5+
import time
6+
import smtplib
7+
import ssl
8+
9+
10+
class Scraper:
11+
12+
# Initializes the scraper C3PO
13+
def __init__(self, url, budget, u_email):
14+
15+
# Attributes about product
16+
self.url = url
17+
self.budget = budget
18+
19+
# Setting user email
20+
self.u_email = u_email
21+
22+
# Attributes about scraping
23+
self.session = HTMLSession()
24+
self.webpage = self.session.get(self.url).content
25+
self.parser = 'lxml'
26+
self.soup = BeautifulSoup(self.webpage, self.parser)
27+
28+
# Prints the object
29+
def __str__(self):
30+
return self.soup.prettify()
31+
32+
# Stores the title of the product
33+
def get_title(self):
34+
temp_title = self.soup.find('span', id='productTitle').text.strip()
35+
temp_list_title = []
36+
for x in temp_title:
37+
if x == '(':
38+
break
39+
temp_list_title.append(x)
40+
self.product_title = ''.join(temp_list_title)
41+
return self.product_title
42+
43+
# Stores the price of the product after filtering the string and
44+
# converting it to an integer
45+
def get_price(self):
46+
price_raw = self.soup.find(
47+
'span', id='priceblock_ourprice').text.strip()
48+
price_filtered = price_raw[2:len(price_raw) - 3]
49+
self.product_price = int(
50+
''.join([x for x in price_filtered if x != ',']))
51+
return
52+
53+
# Prints product title
54+
def print_title(self):
55+
print(self.product_title)
56+
return
57+
58+
# Prints product price
59+
def print_price(self):
60+
print(self.product_price)
61+
return
62+
63+
# Checks if the price of the product is below the budget
64+
def is_below_budget(self):
65+
if self.product_price <= self.budget:
66+
return True
67+
else:
68+
return False
69+
70+
# Runs the scraper
71+
def run(self):
72+
73+
self.get_title()
74+
self.get_price()
75+
self.alert = self.is_below_budget()
76+
self.status = False
77+
if self.alert:
78+
self.status = self.send_email()
79+
return self.status
80+
81+
# Sends an email when the condition is satisfied. Under testing!
82+
def send_email(self):
83+
84+
# Attributes for email sending
85+
port = 587
86+
smtp_server = 'smtp.gmail.com'
87+
self.email = str(os.environ.get('DEVELOPER_MAIL'))
88+
self.app_pw = str(os.environ.get('DEVELOPER_PASS'))
89+
90+
# Message details
91+
subject = f'The price of {self.get_title()} is within your budget!'
92+
93+
body_start = """Hey there!\n
94+
The price is now within your budget. Here is the link, buy it now!\n"""
95+
body_mid = self.url
96+
body_end = '\n\nRegards\nYour friendly neighbourhood programmer'
97+
body = str(body_start) + str(body_mid) + str(body_end)
98+
99+
message = f"Subject: {subject}\n\n{body}"
100+
101+
# Establishing server
102+
context = ssl.create_default_context()
103+
self.server = smtplib.SMTP(smtp_server, port)
104+
105+
# Mail sending
106+
self.server.ehlo()
107+
self.server.starttls(context=context)
108+
self.server.ehlo()
109+
self.server.login(self.email, self.app_pw)
110+
111+
self.server.sendmail(self.email, self.u_email, message)
112+
113+
print("Email sent successfully!")
114+
self.server.quit()
115+
return True
116+
117+
118+
def main():
119+
url = input(
120+
"Paste the link of the Amazon product:")
121+
budget = int(input("Enter you budget price:"))
122+
u_email = input("Enter your email:")
123+
inp_str = ("How frequuently would you like to check the price?"
124+
"\n1.Every hour\n2.Every 3 hours\n3.Every 6 hours"
125+
"\nEnter your choice(default is 6 hours):")
126+
time_choice = int(input(inp_str))
127+
if time_choice == 1:
128+
time_delay = 60 * 60
129+
elif time_choice == 2:
130+
time_delay = 3 * 60 * 60
131+
else:
132+
time_delay = 6 * 60 * 60
133+
msg = (
134+
"Great! Now just sit back and relax."
135+
"Minimize this program and be sure "
136+
"that it is running.\nAdditionally, ensure that there"
137+
"is stable internet connection "
138+
"during the time this program runs.\nIf the price of the "
139+
"product falls within your budget, "
140+
"you will recieve an email regarding the same and this"
141+
"program will auto-close.\nThank you for using "
142+
"C3PO scraper! Beep-bop bop-beep.")
143+
print(msg)
144+
c3po = Scraper(url, budget, u_email)
145+
while True:
146+
if c3po.run():
147+
break
148+
time.sleep(time_delay)
149+
150+
151+
if __name__ == '__main__':
152+
main()

‎amazon-price-alert/requirements.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
appdirs==1.4.4
2+
beautifulsoup4==4.9.1
3+
bs4==0.0.1
4+
certifi==2020年6月20日
5+
chardet==3.0.4
6+
cssselect==1.1.0
7+
fake-useragent==0.1.11
8+
idna==2.10
9+
lxml==4.5.2
10+
parse==1.18.0
11+
pyee==7.0.4
12+
pyppeteer==0.2.2
13+
pyquery==1.4.1
14+
requests==2.24.0
15+
requests-html==0.10.0
16+
six==1.15.0
17+
soupsieve==2.0.1
18+
tqdm==4.49.0
19+
urllib3==1.25.10
20+
w3lib==1.22.0
21+
websockets==8.1

‎amazon-price-alert/screenshot.png

24.5 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /