-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronjob.py
67 lines (54 loc) · 2.28 KB
/
cronjob.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
import requests
from bs4 import BeautifulSoup
import MySQLdb
def strip_non_ascii(string):
''' Returns the string without non ASCII characters'''
stripped = (c for c in string if 48 <= ord(c) <= 57)
return ''.join(stripped)
#--------------------------------------------------
#CRON JOB
#--------------------------------------------------
q = input("Enter product code to Search the product")
## flipkart scrapping ##
fliplink = "https://www.flipkart.com/search?q="+q+"&otracker=start&as-show=on&as=off"
r = requests.get(fliplink)
soup = BeautifulSoup(r.content,"lxml")
price = soup.find_all("div",{"class":"_1vC4OE"})
if price:
fp = price[0].text
fp = strip_non_ascii(fp)
print("Flipkart Price: "+fp)
else:
print("Flipkart do not sell this product")
##amazon scrapping ##
amazonlink = "https://www.amazon.in/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords="+q+"&rh=i%3Aaps%2Ck%3A"+q
#amazonlink = "https://www.amazon.in/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords="+"klv-40w562d&rh"+"=i%3Aaps%2Ck%3A"+"klv-40w562d"
r = requests.get(amazonlink)
soup = BeautifulSoup(r.content,"lxml")
price = soup.find("span",{"class":"a-size-base a-color-price s-price a-text-bold"})
if price:
ap = price.text
ap = strip_non_ascii(ap)
print("Amazon Price: "+ap)
else:
print("Amazon do not sell this product")
ap = -1;
##sanpdeal scrapping##
link = "https://www.snapdeal.com/search?keyword="+q+"&santizedKeyword=&catId=&categoryId=0&suggested=false&vertical=&noOfResults=1&clickSrc=go_header&lastKeyword=&prodCatId=&changeBackToAll=false&foundInAll=false&categoryIdSearched=&cityPageUrl=&categoryUrl=&url=&utmContent=&dealDetail=&sort=rlvncy"
r = requests.get(link)
soup = BeautifulSoup(r.content,"lxml")
price = soup.find_all("span",{"class":"lfloat product-price"})
if price:
sp = price[0].text
sp = strip_non_ascii(sp)
print("Snapdeal Price :"+sp)
else:
print("Snapdeal does not sell this product")
sp= -1;
db = MySQLdb.connect("localhost","root","","oosd")
cursor = db.cursor()
sql = "update product set amazon_price ='"+str(ap)+"',flipkart_price ='"+str(fp)+"',snapdeal_price ='"+str(sp)+"'"+" where p_code ='"+q+"'"
#print(sql)
cursor.execute(sql)
db.commit()
cursor.close()