-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstTry.py
109 lines (84 loc) · 2.68 KB
/
firstTry.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
# import libraries
cnx = mysql.connector.connect(user='root', password='', database='FORTINOS')
cursor = cnx.cursor()
cursor.execute("DROP database FORTINOS")
cursor.execute("CREATE database FORTINOS")
DB_NAME = 'FORTINOS'
TABLES = {}
TABLES['foodInfo'] = (
"CREATE TABLE `foodInfo` ("
" `ID` varchar(100) NOT NULL,"
" `brand` varchar(100) NOT NULL,"
" `name` varchar(100) NOT NULL,"
" `price` decimal(6,2) NOT NULL,"
" PRIMARY KEY (`ID`)"
") ENGINE=InnoDB")
def create_database(cursor):
try:
cursor.execute(
"CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
except mysql.connector.Error as err:
print("Failed creating database: {}".format(err))
exit(1)
try:
cnx.database = DB_NAME
except mysql.connector.Error as err:
if err.errno == errorcode.ER_BAD_DB_ERROR:
create_database(cursor)
cnx.database = DB_NAME
else:
print(err)
exit(1)
for name, ddl in TABLES.iteritems():
try:
print("Creating table {}: ".format(name))
cursor.execute(ddl)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
else:
print("OK")
searchItem = raw_input('Please enter your search term: ')
# specify the url
quote_page = 'https://www.fortinos.ca/search/page/~item/' + searchItem +'/~sort/recommended/~selected/true'
add_data = ("INSERT INTO foodInfo "
"(ID, brand, name, price) "
"VALUES (%(ID)s, %(brand)s, %(name)s, %(price)s)")
page = urllib2.urlopen(quote_page)
# parse the html using beautiful soap and store in variable `soup`
soup=BeautifulSoup(page,'html.parser')
# Take out the <div> of name and get its value
elements = soup.find_all('div', attrs={'class':'item'})
for i in elements:
try:
brand = i.find('span', attrs={'class':'js-product-entry-brand'}).text
except AttributeError:
brand = "NOT FOUND"
try:
name = i.find('span', attrs={'class':'js-product-entry-name'}).text
except AttributeError:
name = "NOT FOUND"
try:
firstPrice = i.find('span', attrs={'class':'reg-price-text'}).text
price = Decimal(firstPrice.split('$')[1])
except AttributeError:
price = 0.00
try:
ID = i.find('div', attrs={'class':'module-grocery-product quickview-simple clicked'}).get('data-product-id')
except AttributeError:
ID = "NOT FOUND"
print brand + " " + name + " " + str(price) + " "
print ID
data = {
'ID': ID,
'brand': brand,
'name': name,
'price': price,
}
cursor.execute(add_data, data)
# Make sure data is committed to the database
cnx.commit()
cursor.close()
cnx.close()