-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
145 lines (90 loc) · 4.08 KB
/
scrape.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
import requests
from bs4 import BeautifulSoup
import threading
import time
import smtplib
import os
from dotenv import load_dotenv
load_dotenv(".env")
class User():
def __init__(self, email, url, price):
self.email = email
self.url = url
self.price = price
self.t = None
def start_thread(self, flag=1):
if flag == 0:
try:
self.url = str(self.url)
website = self.url.split("//")[-1].split('/')[0].split('.')[1]
page = requests.get(self.url)
soup = BeautifulSoup(page.content, 'html.parser')
except:
print("Error Occured!")
if website == 'flipkart':
title = soup.find("span", attrs={"class": "B_NuCI"}).get_text().strip()
price = int(soup.find("div", attrs={"class":"_30jeq3 _16Jk6d"}).get_text()[1:].replace(',', ""))
elif website == 'amazon':
title = soup.find(id="productTitle").get_text().strip()
price = soup.find(id="priceblock_dealprice")
if price != None:
price = int(price.get_text()[1:].split('.')[0].replace(',', ""))
if price == None:
price = int(soup.find("span", attrs={"id":"priceblock_ourprice"}).get_text()[1:].split('.')[0].replace(',', ""))
if price <= self.price:
self.send_mail()
flag=-1
return title, price, flag
else:
while(True):
try:
self.url = str(self.url)
website = self.url.split("//")[-1].split('/')[0].split('.')[1]
page = requests.get(self.url)
soup = BeautifulSoup(page.content, 'html.parser')
except:
print("Error Occured!")
break
if website == 'flipkart':
title = soup.find("span", attrs={"class": "_35KyD6"}).get_text().strip()
price = int(soup.find("div", attrs={"class":"_1vC4OE _3qQ9m1"}).get_text()[1:].replace(',', ""))
elif website == 'amazon':
title = soup.find(id="productTitle").get_text().strip()
price = soup.find(id="priceblock_dealprice")
if price != None:
price = int(price.get_text()[1:].split('.')[0].replace(',', ""))
if price == None:
price = int(soup.find("span", attrs={"id":"priceblock_ourprice"}).get_text()[1:].split('.')[0].replace(',', ""))
if price <= self.price:
self.send_mail()
flag =2
break
if flag==2:
return flag
time.sleep(60*60*60*4)
# def delete_user_from_db(self):
# db.session.query(Database).filter(Database.email== self.email, Database.product_url==self.url).delete()
def scrape(self):
title, price, flag = self.start_thread(flag=0)
if flag== -1:
return title, price
else:
self.t = threading.Thread(target=self.start_thread)
self.t.start()
return title, price
def send_mail(self):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(os.getenv(EMAIL), os.getenv(CRED_EMAIL))
except:
print('Error!!!!')
subject = 'Price fell down!'
body = f"Hey there from ScrapeShop! The price of your product fell down.\nCheck your product link: {self.url}"
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(os.getenv(EMAIL), self.email, msg)
print('Email has been sent')
server.quit()
#if __name__=="__main__":