-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
61 lines (50 loc) · 2.94 KB
/
main.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
import requests
import bs4
import sys
import os
from excelfileconvertion import ConvertExcel
from emailsend import SendMail
from dotenv import load_dotenv
load_dotenv()
search=input("Enter a product name to search :".title())
while search.lower()!='quit':
mail=input("Enter mail id to send filtered product :".title())
min=int(input('Enter minimun price range of the product to filter :'.title()))
max=int(input('Enter maximum price range of the product to filter :'.title()))
url = f"https://www.flipkart.com/search?q={search}&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=off&as=off"
filtered_product_dict={'price':[],'product name':[]}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edge/91.0.864.64",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"DNT": "1"
}
#headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Referer": "https://www.flipkart.com/", "DNT": "1","Connection": "keep-alive" }
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
soup=bs4.BeautifulSoup(response.content,'html.parser')
product=soup.find_all('div',class_='KzDlHZ')
for i,j in enumerate(soup.find_all('div',class_='Nx9bqj _4b5DiR')):
price=int(j.get_text().encode('utf-8').decode(sys.stdout.encoding).replace('₹'.encode('utf-8').decode(sys.stdout.encoding),'').replace(',',''))
if price>=min and price<=max:
filtered_product_dict['price'].append(j.get_text().encode('utf-8').decode(sys.stdout.encoding))
filtered_product_dict['product name'].append(product[i].get_text())
else:
print(f"Failed to access {url}. Status code: {response.status_code}")
except requests.exceptions.ConnectionError as e:
print('Please Turn On Your Network...')
except Exception as e:
print(e)
print(filtered_product_dict)
if filtered_product_dict['price'] and filtered_product_dict['product name']:
xlsx_file_name=search+'_filtered_products.xlsx'
if ConvertExcel(filtered_product_dict,xlsx_file_name):
print('waiting to send email...')
response=SendMail(os.getenv('SENDER_EMAIL'),mail,os.getenv('SENDER_EMAIL_PASSWORD'),f"{search} Filtered Products In Excel Format",xlsx_file_name)
print(response)
else:
print('Convert To Excel Was Failed')
search=input("Enter a product name to search or type 'quit' to exit :".title())