-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebScrapper.py
135 lines (120 loc) · 4.13 KB
/
WebScrapper.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
#In-Built Libs
import smtplib
import time
import os
#Third Party Libs
import requests
from bs4 import BeautifulSoup
from playsound import playsound
#---------------------------------------------------------------------------------------------------------------------------------------------------
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'} #User-Agent
def Price():
while True:
page = requests.get(url, headers = header)
soup = BeautifulSoup(page.content, 'html.parser')
try:
price = soup.find("span", id = "priceblock_ourprice").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find("span", class_ = "a-offscreen").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find("span", class_ = "a-size-medium a-color-price priceBlockBuyingPriceString").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find(id = "priceblock_ourprice").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find(id = "priceblock_dealprice").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find(id = "buyNewSection").get_text()
return price_slice(price)
except AttributeError:
pass
try:
price = soup.find(id = "priceblock_saleprice").get_text()
return price_slice(price)
except AttributeError:
pass
def price_slice(price):
print(f"Price - {price}")
return price[1:].strip()
def Send_Mail(email,password,remail): #Function for sending a mail to the user
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(email,password)
body = f"Price Fell Down\n\nCheck -{url}"
server.sendmail(email,remail,body)
url = input("Enter Your URL: ")
while True:
try:
page = requests.get(url, headers = header)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id = "productTitle").get_text().strip() #Parsing for the id "productTitle" which contains the Title/Name of the product
except:
continue
print("\n"+title+"\n")
check = input("Is the this the Item You're Looking for? (Y/N)- ")
if check.lower() == 'y':
item_price = Price()
break
else:
print("Try Again\n")
item_price = item_price.replace(',','')
print(item_price)
actual_price = float(item_price)
price_threshold = float(input("Enter Your Min Price: "))
print(f"Price Threshold - {price_threshold}")
run_time = int(input("How Often Would You Like to Check?(seconds) - "))
while True:
check = int(input("1.Email\n2.Music Alert: "))
if check == 1:
email = input("Enter Your Mail ID: ")
password = input("Enter your google app password: ")
remail = input("Enter the receivers Email: ")
break
elif check == 2:
music_path = input("Enter a Path to Your Music: ")
break
else:
print("Wrong Input!\n")
if check == 1:
while True:
item_price = Price()
item_price = item_price.replace(',','')
actual_price = float(item_price)
if price_threshold >= actual_price:
Send_Mail(email,password,remail)
print("EMAIL SENT")
break
else:
print("Running...\n")
time.sleep(run_time)
else:
while True:
item_price = Price()
item_price = item_price.replace(',','')
actual_price = float(item_price)
if price_threshold >= actual_price:
playsound(music_path)
print("The Price has Dropped!\n")
break
else:
print("Running...\n")
time.sleep(run_time)
playsound(music_path)
if __name__ == '__main__':
pass