-
Notifications
You must be signed in to change notification settings - Fork 0
/
flipkartUpdate.py
47 lines (41 loc) · 1.11 KB
/
flipkartUpdate.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
import MySQLdb
import time
import urllib2
from bs4 import BeautifulSoup
def databaseConnect():
username='root'
password='cassandra'
database='mysmartprice'
db = MySQLdb.connect("localhost",username,password,database)
return db
def findTagValueInSoup(soup, tagName, attrsDict):
element = soup.find(tagName, attrsDict)
if element:
elementValue = element.get_text().encode("ascii","ignore").strip()
else:
elementValue = "NA"
return elementValue
def scrapflipkart(flipkartURL):
urlSourceCode = urllib2.urlopen(flipkartURL.strip())
soup = BeautifulSoup(urlSourceCode)
productPrice = findTagValueInSoup(soup, "span", {"class":"fk-font-verybig pprice fk-bold"})
return productPrice
def main():
db=databaseConnect()
cursor=db.cursor()
#set Table Name Here
tableName='`TABLE 12`'
sql = "SELECT * FROM "+tableName
cursor.execute(sql)
results=cursor.fetchall()
for row in results:
try:
newPrice=scrapflipkart(row[1])
sql='update '+tableName+' set productPrice="'+newPrice+'" where FK_URL="'+row[1]+'"'
cursor.execute(sql)
except:
print row[3]
db.commit()
db.close()
if __name__ == '__main__':
main()