forked from dilrajsingh1997/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapdeal_results.py
47 lines (42 loc) · 1.4 KB
/
snapdeal_results.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
from bs4 import BeautifulSoup
import urllib2
import re
from tabulate import tabulate
from math import ceil
COLUMNS=["S.no","Product","Original price(Rs.)","Discounted price(Rs.)","Discount(%)"]
def get_results(query):
query = query.replace(' ','+')
my_link=urllib2.urlopen("http://www.snapdeal.com/search?keyword="+query)
soup=BeautifulSoup(my_link,'lxml')
links=[]
results=[]
count=1
pricedata=['product-desc-price','product-price']
for product in soup.findAll(attrs={'class':'product-tuple-description'}):
k=[]
k.append(count)
links.append(product.a['href'])
k.append(product.p['title'][:50])
for i in range(2):
flag=0
for prices in product.findAll(attrs={'class':pricedata[i]}):
price=prices.get_text().encode("utf-8")
price = re.sub('[^0-9]+','', price)
k.append(int(price))
flag=1
break
if (flag == 0):
k.append(0)
if(k[2] == 0):
k[2]=k[3]
k.append(ceil((float(k[2]-k[3])/k[2])*100))
results.append(k)
count+=1
if(count>10):
break
return results
if __name__ == "__main__":
query = raw_input("What do you want to search for? ")
results=get_results(query)
print tabulate(results,headers=COLUMNS,tablefmt="fancy_grid")
#print links