-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (101 loc) · 4.51 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
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
import requests
from bs4 import BeautifulSoup
import pandas as pd
from links_list import urls
import os
import time
products = {}
locations = {
'kochi': {},
'thiruvandapuram': {},
"coimbatore": {},
}
def get_product_details(url):
for product_name, url in url.items():
product_name = product_name
url = url
vendor = url.split('.')[1]
request = requests.get(url)
soup = BeautifulSoup(request.content, 'lxml')
if vendor == "luluhypermarket":
price = soup.findAll('span', class_="item price")[0].text.split()[-1]
product_mass = soup.findAll('h1', class_='product-name')[0].text.strip().split()[-1]
location = "kochi"
elif vendor == "klfresh":
price_and_mass = soup.find('span', class_="t3-mainPrice mr-5").text.split()
product_mass = price_and_mass[3]
price = price_and_mass[1]
location = "kochi"
elif vendor == "kada":
product_details = soup.find("div", "rightDetailHolder")
product_mass = product_details.h2.get_text().split()[-1]
price = product_details.find("span", class_="rupee_symbol").text
location = "thiruvandapuram"
elif vendor == "jiomart":
product_mass = '1 kg' # have to fix mass problem Jio Mart Beta
price = soup.find_all("span", class_="final-price")
for i in price:
price = i.text.split(":")[-1].replace('₹', '')
location = "kochi"
elif vendor == "bigbasket":
product_details = soup.find_all("h1", class_="GrE04")
product_mass = product_details[0].text.split(',')[-1]
price = soup.find_all("td", class_="IyLvo")
price = price[0].get_text().split()[-1]
location = "kochi"
elif vendor == 'findfresh':
price = soup.find('p', class_='reduced').text.split()[-1].replace('₹', '')
product_mass = soup.find('div', 'single-right').h3.text.split('(')[-1].replace(')', '')
location = 'kochi'
if price == []:
price = '0'
return product_name,product_mass,float(price),location,vendor
for url in urls:
product_name, product_mass, price, location, vendor = get_product_details(url)
products[product_name] = product_mass
details = {
'price': price,
'mass': product_mass,
}
if location in locations:
if vendor in locations[location]:
locations[location][vendor].update({product_name:details})
else:
locations[location][vendor] = {product_name:details}
my_list = []
my_list.append(['Name', 'Mass', '', 'Coimbatore', '', '', '', 'Tvm', '', '', '', '', 'Kochi', '', ''])
my_list.append(['', '', 'BigBasket', 'JioMart', 'VegRoot', '', 'kada.in', 'amneeds', 'JioMart', '', 'BigBasket', 'JioMart','KL Fresh', 'Lulu Hypermart', 'findfresh'])
print(products)
for product_name,mass in products.items():
my_list.append([product_name,mass,
# locations['coimbatore']['bigbasket'][product_name]['price'],
0,
# locations['coimbatore']['bigbasket'][product_name]['price'],
0,
# locations['coimbatore']['bigbasket'][product_name]['price'],
0,
'',
# locations['thiruvandapuram']['kada'][product_name]['price'],
0,
# locations['thiruvandapuram']['jiomart'][product_name]['price'],
0,
# locations['thiruvandapuram']['jiomart'][product_name]['price'],
0,
'',
locations['kochi']['bigbasket'][product_name]['price'] if product_name in locations['kochi'][
'bigbasket'] else '0',
locations['kochi']['jiomart'][product_name]['price'] if product_name in locations['kochi'][
'jiomart'] else '0',
locations['kochi']['klfresh'][product_name]['price'] if product_name in locations['kochi'][
'klfresh'] else '0',
locations['kochi']['luluhypermarket'][product_name]['price'] if product_name in locations['kochi'][
'luluhypermarket'] else '0',
locations['kochi']['findfresh'][product_name]['price'] if product_name in locations['kochi'][
'findfresh'] else '0',
])
filename = 'case_array.csv'
path = "/home/loki/CompanyProject/Fetcher"
fullpath = os.path.join(path,filename)
df=pd.DataFrame(my_list)
print(df)
df.to_csv(fullpath, header=False)